intsuc / concat

Concatenates two strings in storage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

concat

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.

Usage

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\""

Algorithm

Fast path

  1. Attempt concatenation with single quotes using the macro '$(first)$(second)'.
    1. If the parsing succeeds and the resulting length matches the sum of the lengths of first and second, this becomes result.
  2. Attempt concatenation with double quotes using the macro "$(first)$(second)".
    1. If the parsing succeeds and the resulting length matches the sum of the lengths of first and second, this becomes result.

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.

Slow path

  1. Decompose first and second into substrings containing only characters that need not to be escaped or a character that needs to be escaped (" or \).
  2. If the last substring of first and the first substring of second are both substrings that don't need to be escaped, compose them.
  3. Generate an escaping string containing only backslashes (\) of a length that exactly survives the composition of these substrings.
  4. 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.
  5. Compose the remaining substrings two by two from the end.
    1. Alternate between composing the two substrings with the escaping string appended to the left side only and to the neither side.
    2. Halve the length of the escaping string after each composition.
    3. Once there is only one substring left, this becomes result.

Complexity

For the concatenated string of first and second, let $N$ be its length, and $M$ be the number of characters that need to be escaped.

Fast path

$\Theta(N)$.

Slow path

$\Theta(N + 2^M)$.

TODO: $\Theta(N + M^2)$ could be achievable with divide-and-conquer composition.

About

Concatenates two strings in storage

License:MIT License


Languages

Language:mcfunction 100.0%