+std::string stripLeftMargin(std::string s) {
+ std::vector<StringPiece> pieces;
+ split("\n", s, pieces);
+ auto piecer = range(pieces);
+
+ auto piece = (piecer.end() - 1);
+ auto needle = std::find_if(piece->begin(),
+ piece->end(),
+ [](char c) { return c != ' ' && c != '\t'; });
+ if (needle == piece->end()) {
+ (piecer.end() - 1)->clear();
+ }
+ piece = piecer.begin();
+ needle = std::find_if(piece->begin(),
+ piece->end(),
+ [](char c) { return c != ' ' && c != '\t'; });
+ if (needle == piece->end()) {
+ piecer.erase(piecer.begin(), piecer.begin() + 1);
+ }
+
+ const auto sentinel = std::numeric_limits<size_t>::max();
+ auto indent = sentinel;
+ size_t max_length = 0;
+ for (piece = piecer.begin(); piece != piecer.end(); piece++) {
+ needle = std::find_if(piece->begin(),
+ piece->end(),
+ [](char c) { return c != ' ' && c != '\t'; });
+ if (needle != piece->end()) {
+ indent = std::min<size_t>(indent, size_t(needle - piece->begin()));
+ } else {
+ max_length = std::max<size_t>(piece->size(), max_length);
+ }
+ }
+ indent = indent == sentinel ? max_length : indent;
+ for (piece = piecer.begin(); piece != piecer.end(); piece++) {
+ if (piece->size() < indent) {
+ piece->clear();
+ } else {
+ piece->erase(piece->begin(), piece->begin() + indent);
+ }
+ }
+ return join("\n", piecer);
+}
+
+} // namespace folly