"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_string = require('../primordials/string.js');
const require_primordials_map_set = require('../primordials/map-set.js');
const require_regexps_hex = require('./hex.js');

//#region src/regexps/spec.ts
/**
* @file Spec-compliant fallback for the TC39 `RegExp.escape`
*   (https://tc39.es/ecma262/#sec-regexp.escape). Implements
*   `escapeRegExpFallback` plus the `isSpecHexEscapeCp` classifier used by both
*   the fallback and downstream callers. The fallback handles every branch of
*   the spec `EncodeForRegExpEscape` algorithm: leading [0-9A-Za-z] are always
*   `\xHH`-escaped (guards against `\0..\9` / `\c` merging in a larger
*   pattern), syntax characters (and `/`) get a plain backslash prefix, the
*   `ControlEscape` set maps to its `\t \n \v \f \r` names, the other
*   punctuators and the whitespace / line-terminator / lone-surrogate code
*   points route through `\xHH` or `\uHHHH`. Every other code point is emitted
*   verbatim.
*/
const SYNTAX_CHARACTERS = new require_primordials_map_set.SetCtor("^$\\.*+?()[]{}|/");
const CONTROL_ESCAPES = new require_primordials_map_set.MapCtor([
	[9, "\\t"],
	[10, "\\n"],
	[11, "\\v"],
	[12, "\\f"],
	[13, "\\r"]
]);
const OTHER_PUNCTUATORS = new require_primordials_map_set.SetCtor(",-=<>#&!%:;@~'`\"");
function escapeRegExpFallback(str) {
	let out = "";
	let isFirst = true;
	for (const char of str) {
		const cp = require_primordials_string.StringPrototypeCodePointAt(char, 0);
		if (isFirst && (cp >= 48 && cp <= 57 || cp >= 65 && cp <= 90 || cp >= 97 && cp <= 122)) out += "\\x" + require_regexps_hex.hex2(cp);
		else if (SYNTAX_CHARACTERS.has(char)) out += "\\" + char;
		else {
			const ctrl = CONTROL_ESCAPES.get(cp);
			if (ctrl !== void 0) out += ctrl;
			else if (isSpecHexEscapeCp(cp)) if (cp <= 255) out += "\\x" + require_regexps_hex.hex2(cp);
			else for (let i = 0; i < char.length; i++) out += "\\u" + require_regexps_hex.hex4(require_primordials_string.StringPrototypeCharCodeAt(char, i));
			else out += char;
		}
		isFirst = false;
	}
	return out;
}
function isSpecHexEscapeCp(cp) {
	if (OTHER_PUNCTUATORS.has(require_primordials_string.StringFromCodePoint(cp))) return true;
	if (cp === 10 || cp === 13 || cp === 8232 || cp === 8233) return true;
	if (cp === 9 || cp === 11 || cp === 12 || cp === 32 || cp === 160 || cp === 65279) return true;
	if (cp >= 55296 && cp <= 57343) return true;
	return false;
}

//#endregion
exports.escapeRegExpFallback = escapeRegExpFallback;
exports.isSpecHexEscapeCp = isSpecHexEscapeCp;