UNPKG

@abaplint/runtime

Version:
67 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.find = find; const throw_error_1 = require("../throw_error"); const types_1 = require("../types"); function find(input) { let val = typeof input.val === "string" ? input.val : input.val.get(); if (input.len !== undefined) { throw "transpiler find(), todo len"; } if (input.regex || input.pcre) { if (input.off !== undefined) { throw "transpiler find(), todo off regex"; } const caseInput = typeof input.case === "string" ? input.case : input.case?.get(); let regex = ""; if (input.regex) { regex = typeof input.regex === "string" ? input.regex : input.regex.get(); } else if (input.pcre) { regex = typeof input.pcre === "string" ? input.pcre : input.pcre.get(); } const flags = caseInput !== "X" ? "i" : ""; const reg = new RegExp(regex, flags); const ret = val.match(reg)?.index; if (ret !== undefined) { return new types_1.Integer().set(ret); } else { return new types_1.Integer().set(-1); } } else { const sub = typeof input.sub === "string" ? input.sub : input.sub?.get(); let off = typeof input.off === "number" ? input.off : input.off?.get() || 0; let occ = typeof input.occ === "number" ? input.occ : input.occ?.get(); if (occ === 0) { (0, throw_error_1.throwError)("CX_SY_STRG_PAR_VAL"); } else if (occ === undefined) { occ = 1; } let negative = false; if (occ < 0) { negative = true; let reversed = ""; // this is faster than doing val.split("").reverse().join("") for (const character of val) { reversed = character + reversed; } val = reversed; occ = Math.abs(occ); } let found = -1; for (let i = 0; i < occ; i++) { found = val.indexOf(sub || "", off); if (found >= 0) { off = found + 1; } } if (negative === true && found >= 0) { found = val.length - found - 1; } return new types_1.Integer().set(found); } } //# sourceMappingURL=find.js.map