fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::string unindent(const char* p)
  5. {
  6. std::string result;
  7. if (p[0] == '\n') ++p;
  8. const char* p_leading = p;
  9. while (std::isspace(*p) && *p != '\n')
  10. ++p;
  11. size_t leading_len = p - p_leading;
  12. while (*p)
  13. {
  14. result += *p;
  15. if (*p == '\n')
  16. {
  17. ++p;
  18. for (size_t i = 0; i < leading_len; ++i)
  19. if (p[i] != p_leading[i])
  20. goto dont_skip_leading;
  21. p += leading_len;
  22. }
  23. else
  24. ++p;
  25. dont_skip_leading: ;
  26. }
  27. return result;
  28. }
  29.  
  30.  
  31. int main() {
  32. std::cout << "[[" << unindent(R"(
  33. This is the first line.
  34. This is the second line.
  35. This is the third line.
  36. )") << "]]\n";
  37.  
  38. std::cout << "[[" << unindent(R"(
  39. This is the first line.
  40. This is the second line, now indented four more spaces.
  41. This is the third line.
  42. This is the fourth line, now indented 2 spaces, which is less than the first.
  43. )") << "]]\n";
  44. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
[[This is the first line.
This is the second line.
This is the third line.
]]
[[This is the first line.
    This is the second line, now indented four more spaces.
This is the third line.
  This is the fourth line, now indented 2 spaces, which is less than the first.
]]