MCPcopy Create free account
hub / github.com/anus-dev/ANUS / unescapeStringForAnusBug

Function unescapeStringForAnusBug

packages/core/src/utils/editCorrector.ts:689–730  ·  view source on GitHub ↗
(inputString: string)

Source from the content-addressed store, hash-verified

687 * Unescapes a string that might have been overly escaped by an LLM.
688 */
689export function unescapeStringForAnusBug(inputString: string): string {
690 // Regex explanation:
691 // \\ : Matches exactly one literal backslash character.
692 // (n|t|r|'|"|`|\\|\n) : This is a capturing group. It matches one of the following:
693 // n, t, r, ', ", ` : These match the literal characters 'n', 't', 'r', single quote, double quote, or backtick.
694 // This handles cases like "\\n", "\\`", etc.
695 // \\ : This matches a literal backslash. This handles cases like "\\\\" (escaped backslash).
696 // \n : This matches an actual newline character. This handles cases where the input
697 // string might have something like "\\\n" (a literal backslash followed by a newline).
698 // g : Global flag, to replace all occurrences.
699
700 return inputString.replace(
701 /\\+(n|t|r|'|"|`|\\|\n)/g,
702 (match, capturedChar) => {
703 // 'match' is the entire erroneous sequence, e.g., if the input (in memory) was "\\\\`", match is "\\\\`".
704 // 'capturedChar' is the character that determines the true meaning, e.g., '`'.
705
706 switch (capturedChar) {
707 case 'n':
708 return '\n'; // Correctly escaped: \n (newline character)
709 case 't':
710 return '\t'; // Correctly escaped: \t (tab character)
711 case 'r':
712 return '\r'; // Correctly escaped: \r (carriage return character)
713 case "'":
714 return "'"; // Correctly escaped: ' (apostrophe character)
715 case '"':
716 return '"'; // Correctly escaped: " (quotation mark character)
717 case '`':
718 return '`'; // Correctly escaped: ` (backtick character)
719 case '\\': // This handles when 'capturedChar' is a literal backslash
720 return '\\'; // Replace escaped backslash (e.g., "\\\\") with single backslash
721 case '\n': // This handles when 'capturedChar' is an actual newline
722 return '\n'; // Replace the whole erroneous sequence (e.g., "\\\n" in memory) with a clean newline
723 default:
724 // This fallback should ideally not be reached if the regex captures correctly.
725 // It would return the original matched sequence if an unexpected character was captured.
726 return match;
727 }
728 },
729 );
730}
731
732/**
733 * Counts occurrences of a substring in a string

Callers 3

ensureCorrectEditFunction · 0.85
ensureCorrectFileContentFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected