{"version":3,"file":"_jsonStringifyString.mjs","names":[],"sources":["../../src/internal/_jsonStringifyString.ts"],"sourcesContent":["/**\n * Serialize a string as a JSON string literal.\n *\n * A string with no control character, surrogate, quote or backslash is wrapped\n * in quotes, and quotes and backslashes are escaped by copying slices; any\n * control character or surrogate falls back to `JSON.stringify`, so the result\n * equals `JSON.stringify(str)` for every input.\n *\n * In the past, name of `typia` was `typescript-json`, and supported JSON\n * serialization by wrapping `fast-json-stringify. `typescript-json`was a helper\n * library of`fast-json-stringify`, which can skip manual JSON schema definition\n * just by putting pure TypeScript type.\n *\n * This `$string` function is a part of `fast-json-stringify` at that time, and\n * still being used in `typia` for the string serialization.\n *\n * @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js\n *\n * @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi\n *\n * @evidence contracts/common.md#principled-implementation The common case is a string without control characters, quotes, backslashes or surrogates, which is wrapped in quotes without a pass through the general serializer; quotes and backslashes are escaped by copying slices, and any control character or surrogate falls back to `JSON.stringify`, which handles every remaining escape and lone-surrogate rule, so the result equals `JSON.stringify(str)` for all input.\n * @evidence contracts/common.md#clear-and-simple-design One function with a single scan and one fallback.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The fast path is a speed-up with the same result and falls back for every character that needs more than escaping a quote or backslash; it is adapted from fast-json-stringify, as the comment records.\n * @evidence contracts/common.md#meaningful-documentation The doc states what the function produces, the origin and the reference of the helper; the equality with `JSON.stringify` is argued in the principle answer.\n */\nexport const _jsonStringifyString = (str: string): string => {\n  const len = str.length;\n  let result = \"\";\n  let last = -1;\n  let point = 255;\n\n  // eslint-disable-next-line\n  for (var i = 0; i < len; i++) {\n    point = str.charCodeAt(i);\n    if (point < 32) {\n      return JSON.stringify(str);\n    }\n    if (point >= 0xd800 && point <= 0xdfff) {\n      // The current character is a surrogate.\n      return JSON.stringify(str);\n    }\n    if (\n      point === 0x22 || // '\"'\n      point === 0x5c // '\\'\n    ) {\n      last === -1 && (last = 0);\n      result += str.slice(last, i) + \"\\\\\";\n      last = i;\n    }\n  }\n\n  return (\n    (last === -1 && '\"' + str + '\"') || '\"' + result + str.slice(last) + '\"'\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,wBAAwB,QAAwB;CAC3D,MAAM,MAAM,IAAI;CAChB,IAAI,SAAS;CACb,IAAI,OAAO;CACX,IAAI,QAAQ;CAGZ,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK;EAC5B,QAAQ,IAAI,WAAW,CAAC;EACxB,IAAI,QAAQ,IACV,OAAO,KAAK,UAAU,GAAG;EAE3B,IAAI,SAAS,SAAU,SAAS,OAE9B,OAAO,KAAK,UAAU,GAAG;EAE3B,IACE,UAAU,MACV,UAAU,IACV;GACA,SAAS,OAAO,OAAO;GACvB,UAAU,IAAI,MAAM,MAAM,CAAC,IAAI;GAC/B,OAAO;EACT;CACF;CAEA,OACG,SAAS,MAAM,OAAM,MAAM,QAAQ,OAAM,SAAS,IAAI,MAAM,IAAI,IAAI;AAEzE"}