MCPcopy Create free account
hub / github.com/couchbase/fleece / patch_fromText

Method patch_fromText

Fleece/Support/diff_match_patch.hh:2265–2351  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2263 */
2264 public:
2265 Patches patch_fromText(const string_t &textline) const {
2266 Patches patches;
2267 if (!textline.empty()) {
2268 char_t sign;
2269 string_t line;
2270 typename string_t::const_pointer text = textline.c_str();
2271 typename string_t::size_type text_len, l;
2272 while (text - textline.c_str() < (ssize_t)textline.length()) {
2273 if ((text_len = next_token(textline, traits::from_wchar(L'\n'), text)) == 0) { ++text; continue; }
2274
2275 // A replacement for the regexp "^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$" exact match
2276 string_t start1, length1, start2, length2;
2277 do {
2278 typename string_t::const_pointer t = text;
2279 l = text_len;
2280 if ((l -= 9) > 0 && traits::to_wchar(*t) == L'@' && traits::to_wchar(*++t) == L'@'
2281 && traits::to_wchar(*++t) == L' ' && traits::to_wchar(*++t) == L'-' && traits::is_digit(*++t)) {
2282 do { start1 += *t; } while (--l > 0 && traits::is_digit(*++t));
2283 if (l > 0 && traits::to_wchar(*t) == L',') ++t, --l;
2284 while (l > 0 && traits::is_digit(*t)) --l, length1 += *t++;
2285 if (l > 0 && traits::to_wchar(*t++) == L' ' && traits::to_wchar(*t++) == L'+' && traits::is_digit(*t)) {
2286 do { start2 += *t; --l; } while (traits::is_digit(*++t));
2287 if (l > 0 && traits::to_wchar(*t) == L',') ++t, --l;
2288 while (l > 0 && traits::is_digit(*t)) --l, length2 += *t++;
2289 if (l == 0 && traits::to_wchar(*t++) == L' ' && traits::to_wchar(*t++) == L'@' && traits::to_wchar(*t) == L'@') break; // Success
2290 }
2291 }
2292 throw string_t(traits::cs(L"Invalid patch string: ") + string_t(text, text_len));
2293 } while (false);
2294
2295 Patch patch;
2296 patch.start1 = to_int(start1);
2297 if (length1.empty()) {
2298 patch.start1--;
2299 patch.length1 = 1;
2300 } else if (length1.size() == 1 && traits::to_wchar(length1[0]) == L'0') {
2301 patch.length1 = 0;
2302 } else {
2303 patch.start1--;
2304 patch.length1 = to_int(length1);
2305 }
2306
2307 patch.start2 = to_int(start2);
2308 if (length2.empty()) {
2309 patch.start2--;
2310 patch.length2 = 1;
2311 } else if (length2.size() == 1 && traits::to_wchar(length2[0]) == L'0') {
2312 patch.length2 = 0;
2313 } else {
2314 patch.start2--;
2315 patch.length2 = to_int(length2);
2316 }
2317
2318 for (text += text_len + 1; text - textline.c_str() < (ssize_t)textline.length(); text += text_len + 1) {
2319 if ((text_len = next_token(textline, traits::from_wchar(L'\n'), text)) == 0) continue;
2320
2321 sign = *text;
2322 line.assign(text + 1, text_len - 1);

Callers 1

applyStringDeltaMethod · 0.80

Calls 4

DiffClass · 0.85
lengthMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected