Skip to content

Commit

Permalink
add plainReplace function
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalMarsalek committed Apr 1, 2024
1 parent 6b0278c commit c57aa1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/common/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { $ } from "./fragments";
import type { TokenTree } from "./Language";
import type { Spine } from "./Spine";
import { codepoints } from "./strings";
import { codepoints, plainReplace } from "./strings";

export function joinTrees(
sep: TokenTree,
Expand Down Expand Up @@ -48,9 +48,9 @@ export function emitTextFactory(
let current = value;
for (const [c, d] of subs) {
if (d === null) continue;
current = current.replaceAll(c, d);
current = plainReplace(current, c, d);
}
current = template.replaceAll("TEXT", current);
current = plainReplace(template, "TEXT", current);
if (!(codepointMap === undefined || (low === 1 && high === Infinity)))
current = codepoints(current)
.map((x, i, arr) =>
Expand Down
11 changes: 11 additions & 0 deletions src/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ export const chars = (str: string) => {

export const byteLength = (x: string | null) =>
x === null ? Infinity : Buffer.byteLength(x, "utf-8");

export function plainReplace(
sourceValue: string,
searchValue: string,
replaceValue: string,
) {
return sourceValue.replaceAll(
searchValue,
replaceValue.replaceAll("$", "$$$$"),
);
}

0 comments on commit c57aa1f

Please sign in to comment.