int begin = 0; // Omit the first empty line. if (omitFirstEmptyLine && s[0] == '\n') { begin = 1; } // Omit the last empty line. // N-2 N-1 N // \n \0 int end = N; if (omitLastEmptyLine && s[N - 2] == '\n') { end = N - 1; }
int minSpaceNum = N; bool newLine = true; int spaceNum = 0; for (int i = begin; i < end; i++) { if (s[i] == '\n' || i == end - 1) { if (minSpaceNum > spaceNum) { minSpaceNum = spaceNum; } newLine = true; spaceNum = 0; continue; } if (s[i] == ' ' && newLine) { spaceNum++; continue; } newLine = false; }
int k = 0; spaceNum = 0; for (int i = begin; i < end - 1; i++) { if (s[i] == '\n') { spaceNum = 0; mArray[k] = s[i]; k++; continue; } if (spaceNum < minSpaceNum) { spaceNum++; continue; } mArray[k] = s[i]; k++; } mArray[k] = '\0';
// Omit the last empty line. if (omitLastEmptyLine && mArray[k - 1] == '\n') { mArray[k - 1] = '\0'; } }
intmain(){ staticconstexprauto a = literal(R"delimiter( test test )delimiter"); staticconstexprauto b = literal(R"delimiter( test test )delimiter"); staticconstexprauto c = literal(R"delimiter( test test )delimiter"); staticconstexprauto d = literal(R"delimiter( test test )delimiter"); staticconstexprauto s = d.c_str(); std::cout << a.c_str() << std::endl; std::cout << b.c_str() << std::endl; std::cout << c.c_str() << std::endl; std::cout << s << std::endl; }