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

Method applyStringDelta

Fleece/Core/JSONDelta.cc:491–532  ·  view source on GitHub ↗

static*/

Source from the content-addressed store, hash-verified

489
490
491 /*static*/ string JSONDelta::applyStringDelta(slice oldStr, slice diff) {
492#if 0
493 // Support for JsonDiffPatch-format string diffs:
494 if (diff[0] == '@') {
495 diff_match_patch<string> dmp;
496 return dmp.patch_apply(dmp.patch_fromText(string(diff)), string(oldStr)).first;
497 }
498#endif
499
500 stringstream in{string(diff)};
501 in.exceptions(stringstream::failbit | stringstream::badbit);
502 stringstream nuu;
503 unsigned pos = 0;
504 while (in.peek() >= 0) {
505 char op;
506 unsigned len;
507 in >> len;
508 in >> op;
509 switch (op) {
510 case '=':
511 throwIf(pos + len > oldStr.size, InvalidData, "Invalid length in text delta");
512 nuu.write((const char*)&oldStr[pos], len);
513 pos += len;
514 break;
515 case '-':
516 pos += len;
517 break;
518 case '+': {
519 TempArray(insertion, char, len);
520 in.read(insertion, len);
521 nuu.write(insertion, len);
522 in >> op;
523 throwIf(op != '|', InvalidData, "Missing insertion delimiter in text delta");
524 break;
525 }
526 default:
527 FleeceException::_throw(InvalidData, "Unknown op in text delta");
528 }
529 }
530 throwIf(pos != oldStr.size, InvalidData, "Length mismatch in text delta");
531 return nuu.str();
532 }
533
534
535 // Given a byte range (`pos`, `length`) in the slice `str`, if either end of the range falls

Callers

nothing calls this directly

Calls 5

patch_applyMethod · 0.80
patch_fromTextMethod · 0.80
peekMethod · 0.45
writeMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected