All files / src/Ice StringToIdentity.js

82.81% Statements 53/64
71.42% Branches 10/14
100% Functions 1/1
82.81% Lines 53/64

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 6541x 41x 41x 41x 41x 41x 41x 41x 413x 413x 413x 413x 413x 413x 413x 413x 413x 58x 58x 13x 13x 58x 58x 58x 58x 58x 53x 53x 53x           53x 58x 58x 413x 413x 360x 360x 360x 360x 3x 3x 413x 53x 53x 53x     53x 53x 53x 53x     53x     53x 410x 410x 413x  
// Copyright (c) ZeroC, Inc.
 
import { ParseException } from "./LocalExceptions.js";
import { StringUtil } from "./StringUtil.js";
import { Ice as Ice_Identity } from "./Identity.js";
const { Identity } = Ice_Identity;
 
export function stringToIdentity(s) {
    const ident = new Identity();
 
    //
    // Find unescaped separator; note that the string may contain an escaped
    // backslash before the separator.
    //
    let slash = -1;
    let pos = 0;
    while ((pos = s.indexOf("/", pos)) !== -1) {
        let escapes = 0;
        while (pos - escapes > 0 && s.charAt(pos - escapes - 1) == "\\") {
            escapes++;
        }
 
        //
        // We ignore escaped escapes
        //
        if (escapes % 2 === 0) {
            if (slash == -1) {
                slash = pos;
            } else {
                //
                // Extra unescaped slash found.
                //
                throw new ParseException(`unescaped backslash in identity '${s}'`);
            }
        }
        pos++;
    }
 
    if (slash == -1) {
        ident.category = "";
        try {
            ident.name = StringUtil.unescapeString(s, 0, s.length, "/");
        } catch (e) {
            throw new ParseException(`invalid identity name '${s}': ${e.toString()}`);
        }
    } else {
        try {
            ident.category = StringUtil.unescapeString(s, 0, slash, "/");
        } catch (e) {
            throw new ParseException(`invalid category in identity '${s}': ${e.toString()}`);
        }
        if (slash + 1 < s.length) {
            try {
                ident.name = StringUtil.unescapeString(s, slash + 1, s.length, "/");
            } catch (e) {
                throw new ParseException(`invalid name in identity '${s}': ${e.toString()}`);
            }
        } else {
            ident.name = "";
        }
    }
 
    return ident;
}