Index: gc_core/js/jsex_map.js ================================================================== --- gc_core/js/jsex_map.js +++ gc_core/js/jsex_map.js @@ -1,43 +1,47 @@ // Map -Map.prototype._shallowCopy = function () { - let oNewMap = new Map(); - for (let [key, val] of this.entries()) { - oNewMap.set(key, val); - } - return oNewMap; -} - -Map.prototype._get = function (key, defaultValue) { - let res = this.get(key); - if (res !== undefined) { - return res; - } - return defaultValue; -} - -Map.prototype._toString = function () { - // Default .toString() gives nothing useful - let sRes = "{ "; - for (let [k, v] of this.entries()) { - sRes += (typeof k === "string") ? '"' + k + '": ' : k.toString() + ": "; - sRes += (typeof v === "string") ? '"' + v + '", ' : v.toString() + ", "; - } - sRes = sRes.slice(0, -2) + " }" - return sRes; -} - -Map.prototype._update = function (dDict) { - for (let [k, v] of dDict.entries()) { - this.set(k, v); - } -} - -Map.prototype._updateOnlyExistingKeys = function (dDict) { - for (let [k, v] of dDict.entries()) { - if (this.has(k)){ - this.set(k, v); - } - } +if (Map.prototype.__grammalecte__ === undefined) { + Map.prototype._shallowCopy = function () { + let oNewMap = new Map(); + for (let [key, val] of this.entries()) { + oNewMap.set(key, val); + } + return oNewMap; + } + + Map.prototype._get = function (key, defaultValue) { + let res = this.get(key); + if (res !== undefined) { + return res; + } + return defaultValue; + } + + Map.prototype._toString = function () { + // Default .toString() gives nothing useful + let sRes = "{ "; + for (let [k, v] of this.entries()) { + sRes += (typeof k === "string") ? '"' + k + '": ' : k.toString() + ": "; + sRes += (typeof v === "string") ? '"' + v + '", ' : v.toString() + ", "; + } + sRes = sRes.slice(0, -2) + " }" + return sRes; + } + + Map.prototype._update = function (dDict) { + for (let [k, v] of dDict.entries()) { + this.set(k, v); + } + } + + Map.prototype._updateOnlyExistingKeys = function (dDict) { + for (let [k, v] of dDict.entries()) { + if (this.has(k)){ + this.set(k, v); + } + } + } + + Map.prototype.__grammalecte__ = true; } Index: gc_core/js/jsex_regex.js ================================================================== --- gc_core/js/jsex_regex.js +++ gc_core/js/jsex_regex.js @@ -1,76 +1,80 @@ // regex -RegExp.prototype._exec2 = function (sText, aGroupsPos, aNegLookBefore=null) { - let m; - while ((m = this.exec(sText)) !== null) { - // we have to iterate over sText here too - // because first match doesn’t imply it’s a valid match according to negative lookbefore assertions, - // and even if first match is finally invalid, it doesn’t mean the following eligible matchs would be invalid too. - if (aNegLookBefore !== null) { - // check negative look before assertions - if ( !aNegLookBefore.some(sRegEx => (RegExp.leftContext.search(sRegEx) >= 0)) ) { - break; - } - } else { - break; - } - } - if (m === null) { - return null; - } - - let codePos; - let iPos = 0; - m.start = [m.index]; - m.end = [this.lastIndex]; - if (m.length > 1) { - // there is subgroup(s) - if (aGroupsPos !== null) { - // aGroupsPos is defined - for (let i = 1; i <= m.length-1; i++) { - codePos = aGroupsPos[i-1]; - if (typeof codePos === "number") { - // position as a number - m.start.push(m.index + codePos); - m.end.push(m.index + codePos + m[i].length); - } else if (codePos === "$") { - // at the end of the pattern - m.start.push(this.lastIndex - m[i].length); - m.end.push(this.lastIndex); - } else if (codePos === "w") { - // word in the middle of the pattern - iPos = m[0].search("[ ~’,()«»“”]"+m[i]+"[ ,’~()«»“”]") + 1 + m.index - m.start.push(iPos); - m.end.push(iPos + m[i].length) - } else if (codePos === "*") { - // anywhere - iPos = m[0].indexOf(m[i]) + m.index; - m.start.push(iPos); - m.end.push(iPos + m[i].length) - } else if (codePos === "**") { - // anywhere after previous group - iPos = m[0].indexOf(m[i], m.end[i-1]-m.index) + m.index; - m.start.push(iPos); - m.end.push(iPos + m[i].length) - } else if (codePos.startsWith(">")) { - // >x:_ - // todo: look in substring x - iPos = m[0].indexOf(m[i]) + m.index; - m.start.push(iPos); - m.end.push(iPos + m[i].length); - } else { - console.error("# Error: unknown positioning code in regex [" + this.source + "], for group[" + i.toString() +"], code: [" + codePos + "]"); - } - } - } else { - // no aGroupsPos - for (let subm of m.slice(1)) { - iPos = m[0].indexOf(subm) + m.index; - m.start.push(iPos); - m.end.push(iPos + subm.length); - } - } - } - return m; +if (RegExp.prototype.__grammalecte__ === undefined) { + RegExp.prototype._exec2 = function (sText, aGroupsPos, aNegLookBefore=null) { + let m; + while ((m = this.exec(sText)) !== null) { + // we have to iterate over sText here too + // because first match doesn’t imply it’s a valid match according to negative lookbefore assertions, + // and even if first match is finally invalid, it doesn’t mean the following eligible matchs would be invalid too. + if (aNegLookBefore !== null) { + // check negative look before assertions + if ( !aNegLookBefore.some(sRegEx => (RegExp.leftContext.search(sRegEx) >= 0)) ) { + break; + } + } else { + break; + } + } + if (m === null) { + return null; + } + + let codePos; + let iPos = 0; + m.start = [m.index]; + m.end = [this.lastIndex]; + if (m.length > 1) { + // there is subgroup(s) + if (aGroupsPos !== null) { + // aGroupsPos is defined + for (let i = 1; i <= m.length-1; i++) { + codePos = aGroupsPos[i-1]; + if (typeof codePos === "number") { + // position as a number + m.start.push(m.index + codePos); + m.end.push(m.index + codePos + m[i].length); + } else if (codePos === "$") { + // at the end of the pattern + m.start.push(this.lastIndex - m[i].length); + m.end.push(this.lastIndex); + } else if (codePos === "w") { + // word in the middle of the pattern + iPos = m[0].search("[ ~’,()«»“”]"+m[i]+"[ ,’~()«»“”]") + 1 + m.index + m.start.push(iPos); + m.end.push(iPos + m[i].length) + } else if (codePos === "*") { + // anywhere + iPos = m[0].indexOf(m[i]) + m.index; + m.start.push(iPos); + m.end.push(iPos + m[i].length) + } else if (codePos === "**") { + // anywhere after previous group + iPos = m[0].indexOf(m[i], m.end[i-1]-m.index) + m.index; + m.start.push(iPos); + m.end.push(iPos + m[i].length) + } else if (codePos.startsWith(">")) { + // >x:_ + // todo: look in substring x + iPos = m[0].indexOf(m[i]) + m.index; + m.start.push(iPos); + m.end.push(iPos + m[i].length); + } else { + console.error("# Error: unknown positioning code in regex [" + this.source + "], for group[" + i.toString() +"], code: [" + codePos + "]"); + } + } + } else { + // no aGroupsPos + for (let subm of m.slice(1)) { + iPos = m[0].indexOf(subm) + m.index; + m.start.push(iPos); + m.end.push(iPos + subm.length); + } + } + } + return m; + } + + RegExp.prototype.__grammalecte__ = true; } Index: gc_core/js/jsex_string.js ================================================================== --- gc_core/js/jsex_string.js +++ gc_core/js/jsex_string.js @@ -1,53 +1,57 @@ // String -String.prototype._count = function (sSearch, bOverlapping) { - // http://jsperf.com/string-ocurrence-split-vs-match/8 - if (sSearch.length <= 0) { - return this.length + 1; - } - let nOccur = 0; - let iPos = 0; - let nStep = (bOverlapping) ? 1 : sSearch.length; - while ((iPos = this.indexOf(sSearch, iPos)) >= 0) { - nOccur++; - iPos += nStep; - } - return nOccur; -} -String.prototype._isDigit = function () { - return (this.search(/^[0-9⁰¹²³⁴⁵⁶⁷⁸⁹]+$/) !== -1); -} -String.prototype._isLowerCase = function () { - return (this.search(/^[a-zà-öø-ÿ0-9-]+$/) !== -1); -} -String.prototype._isUpperCase = function () { - return (this.search(/^[A-ZÀ-ÖØ-ߌ0-9-]+$/) !== -1); -} -String.prototype._isTitle = function () { - return (this.search(/^[A-ZÀ-ÖØ-ߌ][a-zà-öø-ÿ'’-]+$/) !== -1); -} -String.prototype._toCapitalize = function () { - return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase(); -} -String.prototype._expand = function (oMatch) { - let sNew = this; - for (let i = 0; i < oMatch.length ; i++) { - let z = new RegExp("\\\\"+parseInt(i), "g"); - sNew = sNew.replace(z, oMatch[i]); - } - return sNew; -} -String.prototype._trimRight = function (sChars) { - let z = new RegExp("["+sChars+"]+$"); - return this.replace(z, ""); -} -String.prototype._trimLeft = function (sChars) { - let z = new RegExp("^["+sChars+"]+"); - return this.replace(z, ""); -} -String.prototype._trim = function (sChars) { - let z1 = new RegExp("^["+sChars+"]+"); - let z2 = new RegExp("["+sChars+"]+$"); - return this.replace(z1, "").replace(z2, ""); +if (String.prototype.__grammalecte__ === undefined) { + String.prototype._count = function (sSearch, bOverlapping) { + // http://jsperf.com/string-ocurrence-split-vs-match/8 + if (sSearch.length <= 0) { + return this.length + 1; + } + let nOccur = 0; + let iPos = 0; + let nStep = (bOverlapping) ? 1 : sSearch.length; + while ((iPos = this.indexOf(sSearch, iPos)) >= 0) { + nOccur++; + iPos += nStep; + } + return nOccur; + } + String.prototype._isDigit = function () { + return (this.search(/^[0-9⁰¹²³⁴⁵⁶⁷⁸⁹]+$/) !== -1); + } + String.prototype._isLowerCase = function () { + return (this.search(/^[a-zà-öø-ÿ0-9-]+$/) !== -1); + } + String.prototype._isUpperCase = function () { + return (this.search(/^[A-ZÀ-ÖØ-ߌ0-9-]+$/) !== -1); + } + String.prototype._isTitle = function () { + return (this.search(/^[A-ZÀ-ÖØ-ߌ][a-zà-öø-ÿ'’-]+$/) !== -1); + } + String.prototype._toCapitalize = function () { + return this.slice(0,1).toUpperCase() + this.slice(1).toLowerCase(); + } + String.prototype._expand = function (oMatch) { + let sNew = this; + for (let i = 0; i < oMatch.length ; i++) { + let z = new RegExp("\\\\"+parseInt(i), "g"); + sNew = sNew.replace(z, oMatch[i]); + } + return sNew; + } + String.prototype._trimRight = function (sChars) { + let z = new RegExp("["+sChars+"]+$"); + return this.replace(z, ""); + } + String.prototype._trimLeft = function (sChars) { + let z = new RegExp("^["+sChars+"]+"); + return this.replace(z, ""); + } + String.prototype._trim = function (sChars) { + let z1 = new RegExp("^["+sChars+"]+"); + let z2 = new RegExp("["+sChars+"]+$"); + return this.replace(z1, "").replace(z2, ""); + } + + String.prototype.__grammalecte__ = true; }