const Wikidiff2::String & Wikidiff2::execute(const String & text1, const String & text2, int numContextLines, int maxMovedLines) { // Allocate some result space to avoid excessive copying result.clear(); result.reserve(text1.size() + text2.size() + 10000); // Split input strings into lines StringVector lines1; StringVector lines2; explodeLines(text1, lines1); explodeLines(text2, lines2); // Do the diff diffLines(lines1, lines2, numContextLines, maxMovedLines); // Return a reference to the result buffer return result; }
std::string removeBlankLines(std::string s) { Vector<std::string> lines = explodeLines(s); Vector<std::string> newLines; for (std::string line : lines) { line = trimR(line); if (line.length() > 0) { newLines.add(line); } } return implode(newLines, "\n"); }
std::string indent(std::string s, int spaces) { Vector<std::string> lines = explodeLines(s); Vector<std::string> newLines; std::string indentStr = ""; for (int i = 0; i < spaces; i++) { indentStr += " "; } for (std::string line : lines) { newLines.add(indentStr + line); } return implode(newLines); }
std::string trimToWidth(std::string s, int width, std::string suffix) { Vector<std::string> lines = explodeLines(s); for (int i = 0; i < lines.size(); i++) { if ((int) lines[i].length() > width) { lines[i] = lines[i].substr(0, width); if (!suffix.empty()) { lines[i] += suffix; } } } return implode(lines); }
std::string trimToHeight(std::string s, int height, std::string suffix) { Vector<std::string> lines = explodeLines(s); int lineCount = lines.size(); bool wasTooTall = lineCount > height; while (lines.size() > height) { lines.remove(lines.size() - 1); } if (wasTooTall && !suffix.empty()) { lines.add(suffix + " (" + integerToString(lineCount - height) + " line(s) truncated)"); } return implode(lines); }