Warning This data pack is intended to demonstrate the feasibility of concatenating strings that need to be escaped in storage without relying on component updates, and is not intended to be used in production.
Concatenates two strings in storage.
data modify storage concat: first set value "'hello' \\ "
data modify storage concat: second set value '"world"'
function concat:concat
data get storage concat: result
#> Storage concat: has the following contents: "'hello' \\ \"world\""
- Attempt concatenation with single quotes using the macro
'$(first)$(second)'
.- If the parsing succeeds and the resulting length matches the sum of the lengths of
first
andsecond
, this becomesresult
.
- If the parsing succeeds and the resulting length matches the sum of the lengths of
- Attempt concatenation with double quotes using the macro
"$(first)$(second)"
.- If the parsing succeeds and the resulting length matches the sum of the lengths of
first
andsecond
, this becomesresult
.
- If the parsing succeeds and the resulting length matches the sum of the lengths of
Note The second condition is necessary because the parsing can succeed and compose two characters accidentally if the concatenated string contains one or more valid escape sequences.
- Decompose
first
andsecond
into substrings containing only characters that need not to be escaped or a character that needs to be escaped ("
or\
). - If the last substring of
first
and the first substring ofsecond
are both substrings that don't need to be escaped, compose them. - Generate an escaping string containing only backslashes (
\
) of a length that exactly survives the composition of these substrings. - If the last substring is a substring that needs to be escaped (
"
or\
), compose the last two substrings with the escaping string appended to the right side only. - Compose the remaining substrings two by two from the end.
- Alternate between composing the two substrings with the escaping string appended to the left side only and to the neither side.
- Halve the length of the escaping string after each composition.
- Once there is only one substring left, this becomes
result
.
For the concatenated string of first
and second
, let
TODO:
$\Theta(N + M^2)$ could be achievable with divide-and-conquer composition.