All files / src/Ice StringUtil.js

80.64% Statements 425/527
77.21% Branches 122/158
100% Functions 11/11
80.64% Lines 425/527

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 52841x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 465x 465x 2851x 2851x 322x 322x 2851x 143x 465x 41x 41x 41x 41x 41x 41x 41x 861x 861x 838x 838x 718x 718x 838x 143x 861x 41x 41x 41x 41x 41x 3247x 3247x 3247x 3241x     3241x 3247x 3247x 3247x 3247x 3247x 4x 4x 4x 37x 37x 37x 3247x 3243x 23807x 23807x 23805x 23807x 2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 2x 2x 2x 23807x 3243x 3247x 3247x 41x 41x 41x 41x 41x 41x 516x 516x 516x 516x 516x 516x 516x 462x 462x     462x 462x 516x 516x 516x 516x 492x 492x 2704x 2704x 491x 516x 24x 24x 227x 227x 22x 22x 516x 41x 41x 41x 41x 41x 259x 259x 259x 259x 259x 3042x 2x 2x 3042x 3040x 3040x 3040x   3040x   3040x 3040x 3040x 3040x   3040x   3040x 2x 2x 2x 3040x 466x 466x 466x 466x 466x 466x 466x 466x 466x 2572x 2572x 2572x 2572x 3042x 259x 259x 258x 258x 259x     259x 259x 259x 41x 41x 41x 41x 41x 41x 41x 41x 420x 420x 420x 420x 21x 21x 21x 18x 18x 18x     3x 3x 399x 420x 41x 41x 22002x 22002x 94576x 94576x 22002x 22002x 41x 41x 41x 41x 41x 2531x 2531x     2531x 2531x 2531x 2531x 2531x     2529x 2531x 41x 41x 23842x 23842x 23842x 5x 5x 5x 5x 23842x         23842x         23842x                   23842x         23842x         23842x         23842x         23842x         23842x                   23842x 23837x 23837x 23837x 8x 8x 23837x 36x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 18x 18x 9x 9x 9x 9x 16x 16x 9x 9x 9x 9x 9x 23829x 23793x 23793x 23793x 23837x 23837x 23842x 23842x 41x 2896x 2896x 2896x 1x 1x 1x 1x     1x 1x 1x 2895x 2896x 41x 41x 41x 41x 41x 227x 227x 227x 227x 227x 227x 187x 227x     40x 40x 40x 40x 40x 40x 40x 40x 8x 8x 8x 8x 40x         40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x         40x 40x 11x 11x 11x 11x 11x 52x 52x 38x 52x 14x 14x         52x 52x 52x 11x     11x 1x 1x 11x 8x 11x 2x 2x 10x 10x 40x 40x 40x 40x 40x 40x 40x 40x 40x 11x 11x 11x 11x 11x 25x 25x                                       25x 25x 74x 74x 3x 3x 3x 3x 71x 71x 25x 1x 1x 25x 24x 24x 24x 24x 25x 14x 14x 14x 14x 14x 14x 14x 25x 10x 10x 10x 10x 10x 40x 5x     5x 5x 5x 40x 40x 225x 225x 227x  
// Copyright (c) ZeroC, Inc.
 
import { ToStringMode } from "./ToStringMode.js";
 
export class StringUtil {
    //
    // Return the index of the first character in str to
    // appear in match, starting from start. Returns -1 if none is
    // found.
    //
    static findFirstOf(str, match, start) {
        start = start === undefined ? 0 : start;
        for (let i = start; i < str.length; i++) {
            const ch = str.charAt(i);
            if (match.indexOf(ch) != -1) {
                return i;
            }
        }
        return -1;
    }
 
    //
    // Return the index of the first character in str which does
    // not appear in match, starting from start. Returns -1 if none is
    // found.
    //
    static findFirstNotOf(str, match, start) {
        start = start === undefined ? 0 : start;
        for (let i = start; i < str.length; i++) {
            const ch = str.charAt(i);
            if (match.indexOf(ch) == -1) {
                return i;
            }
        }
        return -1;
    }
 
    //
    // Add escape sequences (such as "\n", or "\123") to s
    //
    static escapeString(s, special, toStringMode) {
        special = special === undefined ? null : special;
        if (special !== null) {
            for (let i = 0; i < special.length; ++i) {
                if (special.charCodeAt(i) < 32 || special.charCodeAt(i) > 126) {
                    throw new RangeError("special characters must be in ASCII range 32-126");
                }
            }
        }
 
        const result = [];
 
        if (toStringMode === ToStringMode.Compat) {
            // Encode UTF-8 bytes
            const bytes = unescape(encodeURIComponent(s));
            for (let i = 0; i < bytes.length; ++i) {
                const c = bytes.charCodeAt(i);
                encodeChar(c, result, special, toStringMode);
            }
        } else {
            for (let i = 0; i < s.length; ++i) {
                const c = s.charCodeAt(i);
                if (toStringMode === ToStringMode.Unicode || c < 0xd800 || c > 0xdfff) {
                    encodeChar(c, result, special, toStringMode);
                } else {
                    console.assert(toStringMode === ToStringMode.ASCII && c >= 0xd800 && c <= 0xdfff);
                    if (i + 1 === s.length) {
                        throw new RangeError("High surrogate without low surrogate");
                    } else {
                        const codePoint = s.codePointAt(i);
                        console.assert(codePoint > 0xffff);
                        i++;
 
                        // append \Unnnnnnnn
                        result.push("\\U");
                        const hex = codePoint.toString(16);
                        for (let j = hex.length; j < 8; j++) {
                            result.push("0");
                        }
                        result.push(hex);
                    }
                }
            }
        }
        return result.join("");
    }
 
    //
    // Remove escape sequences added by escapeString. Throws Error
    // for an invalid input string.
    //
    static unescapeString(s, start, end, special) {
        start = start === undefined ? 0 : start;
        end = end === undefined ? s.length : end;
        special = special === undefined ? null : special;
 
        console.assert(start >= 0 && start <= end && end <= s.length);
 
        if (special !== null) {
            for (let i = 0; i < special.length; ++i) {
                if (special.charCodeAt(i) < 32 || special.charCodeAt(i) > 126) {
                    throw new RangeError("special characters must be in ASCII range 32-126");
                }
            }
        }
 
        // Optimization for strings without escapes
        let p = s.indexOf("\\", start);
        if (p == -1 || p >= end) {
            p = start;
            while (p < end) {
                checkChar(s, p++);
            }
            return s.substring(start, end);
        } else {
            const arr = [];
            while (start < end) {
                start = decodeChar(s, start, end, special, arr);
            }
            return arr.join("");
        }
    }
 
    //
    // Split string helper; returns null for unmatched quotes
    //
    static splitString(str, delim) {
        const v = [];
        let s = "";
        let pos = 0;
        let quoteChar = null;
        while (pos < str.length) {
            if (quoteChar === null && (str.charAt(pos) === '"' || str.charAt(pos) === "'")) {
                quoteChar = str.charAt(pos++);
                continue; // Skip the quote.
            } else if (
                quoteChar === null &&
                str.charAt(pos) === "\\" &&
                pos + 1 < str.length &&
                (str.charAt(pos + 1) === '"' || str.charAt(pos + 1) === "'")
            ) {
                ++pos; // Skip the backslash
            } else if (
                quoteChar !== null &&
                str.charAt(pos) === "\\" &&
                pos + 1 < str.length &&
                str.charAt(pos + 1) === quoteChar
            ) {
                ++pos; // Skip the backslash
            } else if (quoteChar !== null && str.charAt(pos) === quoteChar) {
                ++pos;
                quoteChar = null;
                continue; // Skip the quote.
            } else if (delim.indexOf(str.charAt(pos)) !== -1) {
                if (quoteChar === null) {
                    ++pos;
                    if (s.length > 0) {
                        v.push(s);
                        s = "";
                    }
                    continue;
                }
            }
 
            if (pos < str.length) {
                s += str.charAt(pos++);
            }
        }
 
        if (s.length > 0) {
            v.push(s);
        }
        if (quoteChar !== null) {
            return null; // Unmatched quote.
        }
 
        return v;
    }
 
    //
    // If a single or double quotation mark is found at the start position,
    // then the position of the matching closing quote is returned. If no
    // quotation mark is found at the start position, then 0 is returned.
    // If no matching closing quote is found, then -1 is returned.
    //
    static checkQuote(s, start) {
        start = start === undefined ? 0 : start;
 
        const quoteChar = s.charAt(start);
        if (quoteChar == '"' || quoteChar == "'") {
            start++;
            let pos;
            while (start < s.length && (pos = s.indexOf(quoteChar, start)) != -1) {
                if (s.charAt(pos - 1) != "\\") {
                    return pos;
                }
                start = pos + 1;
            }
            return -1; // Unmatched quote
        }
        return 0; // Not quoted
    }
 
    static hashCode(s) {
        let hash = 0;
        for (let i = 0; i < s.length; i++) {
            hash = 31 * hash + s.charCodeAt(i);
        }
        return hash;
    }
 
    // Converts a string to an int32 number. We avoid using parseInt or the Number constructor because they do not
    // detect overflow. This method throws a RangeError if the input is not a valid integer string or if the value
    // is outside the 32-bit signed integer range.
    static toInt32(s) {
        // Reject empty strings and strings with leading or trailing whitespace.
        if (s.length === 0 || s !== s.trim()) {
            throw new RangeError(`conversion of '${s}' to int failed`);
        }
 
        const b = BigInt(s);
        const int32MinValue = -2147483648n;
        const int32MaxValue = 2147483647n;
        if (b < int32MinValue || b > int32MaxValue) {
            throw new RangeError(`Value out of Int32 range: ${s}`);
        }
        return Number(b);
    }
}
 
function encodeChar(c, sb, special, toStringMode) {
    switch (c) {
        case 92: {
            // '\\'
            sb.push("\\\\");
            break;
        }
        case 39: {
            // '\''
            sb.push("\\'");
            break;
        }
        case 34: {
            // '"'
            sb.push('\\"');
            break;
        }
        case 7: {
            // '\a'
            if (toStringMode == ToStringMode.Compat) {
                // Octal escape for compatibility with 3.6 and earlier
                sb.push("\\007");
            } else {
                sb.push("\\a");
            }
            break;
        }
        case 8: {
            // '\b'
            sb.push("\\b");
            break;
        }
        case 12: {
            // '\f'
            sb.push("\\f");
            break;
        }
        case 10: {
            // '\n'
            sb.push("\\n");
            break;
        }
        case 13: {
            // '\r'
            sb.push("\\r");
            break;
        }
        case 9: {
            // '\t'
            sb.push("\\t");
            break;
        }
        case 11: {
            // '\v'
            if (toStringMode == ToStringMode.Compat) {
                // Octal escape for compatibility with 3.6 and earlier
                sb.push("\\013");
            } else {
                sb.push("\\v");
            }
            break;
        }
        default: {
            const s = String.fromCharCode(c);
 
            if (special !== null && special.indexOf(s) !== -1) {
                sb.push("\\");
                sb.push(s);
            } else if (c < 32 || c > 126) {
                if (toStringMode === ToStringMode.Compat) {
                    //
                    // When ToStringMode=Compat, c is a UTF-8 byte
                    //
                    console.assert(c < 256);
                    sb.push("\\");
                    const octal = c.toString(8);
                    //
                    // Add leading zeroes so that we avoid problems during
                    // decoding. For example, consider the encoded string
                    // \0013 (i.e., a character with value 1 followed by
                    // the character '3'). If the leading zeroes were omitted,
                    // the result would be incorrectly interpreted by the
                    // decoder as a single character with value 11.
                    //
                    for (let j = octal.length; j < 3; j++) {
                        sb.push("0");
                    }
                    sb.push(octal);
                } else if (c < 32 || c == 127 || toStringMode === ToStringMode.ASCII) {
                    // append \\unnnn
                    sb.push("\\u");
                    const hex = c.toString(16);
                    for (let j = hex.length; j < 4; j++) {
                        sb.push("0");
                    }
                    sb.push(hex);
                } else {
                    // keep as is
                    sb.push(s);
                }
            } else {
                // printable ASCII character
                sb.push(s);
            }
            break;
        }
    }
}
 
function checkChar(s, pos) {
    const c = s.charCodeAt(pos);
    if (c < 32 || c === 127) {
        let msg;
        if (pos > 0) {
            msg = "character after `" + s.substring(0, pos) + "'";
        } else {
            msg = "first character";
        }
        msg += " has invalid ordinal value" + c;
        throw new RangeError(msg);
    }
    return s.charAt(pos);
}
//
// Decode the character or escape sequence starting at start and appends it to result;
// returns the index of the first character following the decoded character
// or escape sequence.
//
function decodeChar(s, start, end, special, result) {
    console.assert(start >= 0);
    console.assert(start < end);
    console.assert(end <= s.length);
 
    if (s.charAt(start) != "\\") {
        result.push(checkChar(s, start++));
    } else if (start + 1 === end) {
        ++start;
        result.push("\\"); // trailing backslash
    } else {
        let c = s.charAt(++start);
 
        switch (c) {
            case "\\":
            case "'":
            case '"':
            case "?": {
                ++start;
                result.push(c);
                break;
            }
            case "a": {
                ++start;
                result.append("\u0007");
                break;
            }
            case "b": {
                ++start;
                result.push("\b");
                break;
            }
            case "f": {
                ++start;
                result.push("\f");
                break;
            }
            case "n": {
                ++start;
                result.push("\n");
                break;
            }
            case "r": {
                ++start;
                result.push("\r");
                break;
            }
            case "t": {
                ++start;
                result.push("\t");
                break;
            }
            case "v": {
                ++start;
                result.push("\v");
                break;
            }
            case "u":
            case "U": {
                let codePoint = 0;
                const inBMP = c === "u";
                let size = inBMP ? 4 : 8;
                ++start;
                while (size > 0 && start < end) {
                    let charVal = s.charCodeAt(start++);
                    if (charVal >= 0x30 && charVal <= 0x39) {
                        charVal -= 0x30;
                    } else if (charVal >= 0x61 && charVal <= 0x66) {
                        charVal += 10 - 0x61;
                    } else if (charVal >= 0x41 && charVal <= 0x46) {
                        charVal += 10 - 0x41;
                    } else {
                        break; // while
                    }
                    codePoint = codePoint * 16 + charVal;
                    --size;
                }
                if (size > 0) {
                    throw new RangeError("Invalid universal character name: too few hex digits");
                }
                if (codePoint >= 0xd800 && codePoint <= 0xdfff) {
                    throw new RangeError("A universal character name cannot designate a surrogate");
                }
                if (inBMP || codePoint <= 0xffff) {
                    result.push(String.fromCharCode(codePoint));
                } else {
                    result.push(String.fromCodePoint(codePoint));
                }
                break;
            }
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "x": {
                // UTF-8 byte sequence encoded with octal or hex escapes
 
                const arr = [];
                let more = true;
                while (more) {
                    let val = 0;
                    if (c === "x") {
                        let size = 2;
                        ++start;
                        while (size > 0 && start < end) {
                            let charVal = s.charCodeAt(start++);
                            if (charVal >= 0x30 && charVal <= 0x39) {
                                charVal -= 0x30;
                            } else if (charVal >= 0x61 && charVal <= 0x66) {
                                charVal += 10 - 0x61;
                            } else if (charVal >= 0x41 && charVal <= 0x46) {
                                charVal += 10 - 0x41;
                            } else {
                                break; // while
                            }
                            val = val * 16 + charVal;
                            --size;
                        }
                        if (size === 2) {
                            throw new RangeError("Invalid \\x escape sequence: no hex digit");
                        }
                    } else {
                        for (let j = 0; j < 3 && start < end; ++j) {
                            const charVal = s.charCodeAt(start++) - "0".charCodeAt(0);
                            if (charVal < 0 || charVal > 7) {
                                --start; // move back
                                console.assert(j !== 0); // must be at least one digit
                                break; // for
                            }
                            val = val * 8 + charVal;
                        }
                        if (val > 255) {
                            throw new RangeError("octal value \\" + val.toString(8) + " (" + val + ") is out of range");
                        }
                    }
 
                    arr.push(String.fromCharCode(val));
 
                    more = false;
                    if (start + 1 < end && s.charAt(start) === "\\") {
                        c = s.charAt(start + 1);
                        const charVal = s.charCodeAt(start + 1);
                        if (c === "x" || (charVal >= 0x30 && charVal <= 0x39)) {
                            start++;
                            more = true;
                        }
                    }
                }
 
                // Decode UTF-8 arr into string
                result.push(decodeURIComponent(escape(arr.join(""))));
                break;
            }
            default: {
                if (special === null || special.length === 0 || special.indexOf(c) === -1) {
                    result.push("\\"); // not in special, so we keep the backslash
                }
                result.push(checkChar(s, start++));
                break;
            }
        }
    }
 
    return start;
}