apply source patches is Ok text, where text is source with every patch applied. source is the bytes of the file that the positions were recorded in. The caller must pass the patches of that file alone, because the file of a position is compared with nothing.
Decoding. The literal that follows the position decodes as the compiler reads it under OCaml 5.2 and later, with its escapes and continuation lines resolved and a CR LF newline read as LF. OCaml 5.0 and 5.1 keep the CR of such a newline in the compiled value. No version drops more than one CR before an LF, and the decoder drops them all. In both cases the patch is refused as Drifted.
Rewriting. The literal is replaced by one that holds the new contents in its own delimiter. A quoted literal stays quoted. Each line is escaped by String.escaped and the lines are joined by \n, so the literal is one source line and its non-ASCII bytes become decimal escapes. A {tag|…|tag} literal keeps its tag and its contents are written raw. The tag grows by xxx until the contents hold neither {tag| nor |tag}.
The head of a {%expect tag|…|tag} node is kept. A node without payload gets a space and a {|…|} literal before its closing bracket.
The new contents of an Exact patch that hold a CR are written in a quoted literal, whatever the delimiter of the old one, and the CR is written \r. A {%ext|…|} node becomes [%ext "…"], and a node without payload gets a space and the quoted literal.
The new contents of an Exact patch are its content as given. Those of a Flexible patch are the lines of normalize content, laid out as follows, where c is the number of leading spaces of the line that holds the position:
- With no line, the contents are one space in a tagged literal and empty in a quoted one.
- One line stands between two spaces in a tagged literal, and bare in a quoted one.
- Several lines in a tagged literal are a newline, then each line indented by
c + 2 spaces before its own indentation, then a last line of c + 2 spaces, on which the delimiter closes. - Several lines in a quoted literal are a space and a newline, then each line indented by one space before its own indentation, then a newline and a space.
The literal that a Flexible patch writes normalizes to normalize content, so the corrected literal matches on the next run.
The result. The patches apply in the order of their positions, whatever the order of the list. Every byte outside the replaced literals is copied, and in a CR LF file the lines of a new literal end in LF alone. patches must hold one patch at most for a literal.
Error e is the first patch of the list that is refused, and no patch is then applied.