tree-sitter/spec/helpers/dedent.h
Max Brunsfeld 727727623a Start work on unit test that edits python code
Signed-off-by: Nathan Sobo <nathan@github.com>
2016-12-20 13:10:18 -08:00

12 lines
426 B
C++

#include "compiler/util/string_helpers.h"
#include <string>
static std::string dedent(std::string input) {
size_t indent_level = input.find_first_not_of("\n ") - input.find_first_not_of("\n");
std::string whitespace = "\n" + std::string(indent_level, ' ');
tree_sitter::util::str_replace(&input, whitespace, "\n");
return input.substr(
input.find_first_not_of("\n "),
input.find_last_not_of("\n ") + 1
);
}