UNPKG

@magic-xpa/utils

Version:

magic utils package

1,346 lines (1,341 loc) • 110 kB
/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ import { NString, RefParam, StringBuilder } from "@magic-xpa/mscorelib"; import { UtilStrByteMode } from "./UtilStrByteMode"; import { Rtf } from "./Rtf"; /** @type {?} */ var HTML_BACKSLASH = "&#092;"; /** @type {?} */ var HTML_COMMA = "&#044;"; /** @type {?} */ var HTML_HYPHEN = "&#045;"; /** @type {?} */ var STR_2_HTML = 1; /** @type {?} */ export var SEQ_2_HTML = 2; /** @type {?} */ export var HTML_2_STR = 3; /** @type {?} */ var HTML_2_SEQ = 4; /** @type {?} */ export var SEQ_2_STR = 5; var StrUtil = /** @class */ (function () { function StrUtil() { } /// <summary> trim the end of the string</summary> /// <summary> trim the end of the string</summary> /** * @param {?} str * @param {?} len * @return {?} */ StrUtil.mem_trim = /// <summary> trim the end of the string</summary> /** * @param {?} str * @param {?} len * @return {?} */ function (str, len) { /** @type {?} */ var result; if (len > 0) { if (len > str.length) { result = -1; return result; } while (len > 0 && str[len - 1] === ' ') { len = len - 1; } } result = len; return result; }; /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} srcCount * @param {?} len * @return {?} */ StrUtil.memmove = /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} srcCount * @param {?} len * @return {?} */ function (dest, destCount, src, srcCount, len) { /** @type {?} */ var stringBuilder = new StringBuilder(dest.length + len); if (UtilStrByteMode.isLocaleDefLangJPN() && dest.length < destCount) { stringBuilder.Append(NString.FromChar(' ', destCount)); } else { stringBuilder.Append(dest.substr(0, destCount)); } stringBuilder.Append(src.substr(srcCount, len)); if (stringBuilder.Length < dest.length) { stringBuilder.Append(dest.substr(stringBuilder.Length)); } return stringBuilder.ToString(); }; /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} scrCountOrSrcCount * @param {?} count * @return {?} */ StrUtil.memcpy = /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} scrCountOrSrcCount * @param {?} count * @return {?} */ function (dest, destCount, src, scrCountOrSrcCount, count) { if (arguments.length === 5 && (dest === null || dest.constructor === String) && (destCount === null || destCount.constructor === Number) && (src === null || src.constructor === String) && (scrCountOrSrcCount === null || scrCountOrSrcCount.constructor === Number) && (count === null || count.constructor === Number)) { return StrUtil.memcpy_0(dest, destCount, src, scrCountOrSrcCount, count); } StrUtil.memcpy_1(dest, destCount, src, scrCountOrSrcCount, count); }; /// <summary> /// copy part of string into another string, like memcpy of C, but 4 string only /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string</param> /// <param name = "src">string</param> /// <param name = "scrCount">of counter start from in source string</param> /// <param name = "count"></param> /// <returns> new value of destignation string</returns> /// <summary> /// copy part of string into another string, like memcpy of C, but 4 string only /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string</param> /// <param name = "src">string</param> /// <param name = "scrCount">of counter start from in source string</param> /// <param name = "count"></param> /// <returns> new value of destignation string</returns> /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} scrCount * @param {?} count * @return {?} */ StrUtil.memcpy_0 = /// <summary> /// copy part of string into another string, like memcpy of C, but 4 string only /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string</param> /// <param name = "src">string</param> /// <param name = "scrCount">of counter start from in source string</param> /// <param name = "count"></param> /// <returns> new value of destignation string</returns> /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} scrCount * @param {?} count * @return {?} */ function (dest, destCount, src, scrCount, count) { /** @type {?} */ var stringBuilder = new StringBuilder(dest.substr(0, destCount)); if (scrCount + count < src.length) { stringBuilder.Append(src.substr(scrCount, count - scrCount)); } else { stringBuilder.Append(src.substr(scrCount)); } /** @type {?} */ var size = dest.length - destCount - count; if (size > 0) { stringBuilder.Append(dest.substr(destCount + count)); } return stringBuilder.ToString(); }; /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} srcCount * @param {?} count * @return {?} */ StrUtil.memcpy_1 = /** * @param {?} dest * @param {?} destCount * @param {?} src * @param {?} srcCount * @param {?} count * @return {?} */ function (dest, destCount, src, srcCount, count) { while (count > 0 && destCount < dest.length && srcCount < src.length) { dest[destCount++] = src[srcCount++]; count = count - 1; } }; /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ StrUtil.memset = /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ function (dest, destCount, inVal, counter) { if (arguments.length === 4 && (dest === null || dest.constructor === String) && (destCount === null || destCount.constructor === Number) && (inVal === null || inVal.constructor === Number) && (counter === null || counter.constructor === Number)) { return StrUtil.memset_0(dest, destCount, inVal, counter); } StrUtil.memset_1(dest, destCount, inVal, counter); }; /// <summary> /// insert to string chars n times /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string to start insertion of char from</param> /// <param name = "inVal">2 insert</param> /// <param name = "counter">- number of times to insert the char</param> /// <returns> new value of destignation string</returns> /// <summary> /// insert to string chars n times /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string to start insertion of char from</param> /// <param name = "inVal">2 insert</param> /// <param name = "counter">- number of times to insert the char</param> /// <returns> new value of destignation string</returns> /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ StrUtil.memset_0 = /// <summary> /// insert to string chars n times /// </summary> /// <param name = "dest">string</param> /// <param name = "destCount">of counter start from in destignation string to start insertion of char from</param> /// <param name = "inVal">2 insert</param> /// <param name = "counter">- number of times to insert the char</param> /// <returns> new value of destignation string</returns> /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ function (dest, destCount, inVal, counter) { /** @type {?} */ var first = new StringBuilder(dest.substr(0, destCount)); while (counter > 0) { first.Append(inVal); counter = counter - 1; } if (first.Length < dest.length) { first.Append(dest.substr(first.Length)); } return first.ToString(); }; /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ StrUtil.memset_1 = /** * @param {?} dest * @param {?} destCount * @param {?} inVal * @param {?} counter * @return {?} */ function (dest, destCount, inVal, counter) { while (counter > 0 && destCount < dest.length) { dest[destCount++] = inVal; counter = counter - 1; } }; /** * @param {?} str * @param {?} substr * @return {?} */ StrUtil.strstr = /** * @param {?} str * @param {?} substr * @return {?} */ function (str, substr) { /** @type {?} */ var from = str.indexOf(substr); /** @type {?} */ var result; if (from < 0) { result = null; } else { result = str.substr(from); } return result; }; /*******************************/ /// <summary> /// Reverses string values. /// </summary> /// <param name="text">The StringBuilder object containing the string to be reversed.</param> /// <returns>The reversed string contained in a StringBuilder object.</returns> /** * *************************** * @param {?} text * @return {?} */ /// <summary> /// Reverses string values. /// </summary> /// <param name="text">The StringBuilder object containing the string to be reversed.</param> /// <returns>The reversed string contained in a StringBuilder object.</returns> StrUtil.ReverseString = /** * *************************** * @param {?} text * @return {?} */ /// <summary> /// Reverses string values. /// </summary> /// <param name="text">The StringBuilder object containing the string to be reversed.</param> /// <returns>The reversed string contained in a StringBuilder object.</returns> function (text) { // TODO: use string.Reverse() /** @type {?} */ var array = NString.ToCharArray(text.ToString()); array.reverse(); return new StringBuilder(NString.FromChars(array)); }; /// <summary> remove spaces from the right side of string</summary> /// <param name="str">the string to trim /// </param> /// <summary> remove spaces from the right side of string</summary> /// <param name="str">the string to trim /// </param> /** * @param {?} str * @return {?} */ StrUtil.rtrim = /// <summary> remove spaces from the right side of string</summary> /// <param name="str">the string to trim /// </param> /** * @param {?} str * @return {?} */ function (str) { return StrUtil.rtrimWithNull(str, false); }; /// <summary> remove spaces and/or Null chars from the right side of string</summary> /// <param name="str">the string to trim /// </param> /// <param name="trimNullChars">Whether to remove NULL characters or not /// </param> /// <summary> remove spaces and/or Null chars from the right side of string</summary> /// <param name="str">the string to trim /// </param> /// <param name="trimNullChars">Whether to remove NULL characters or not /// </param> /** * @param {?} str * @param {?} trimNullChars * @return {?} */ StrUtil.rtrimWithNull = /// <summary> remove spaces and/or Null chars from the right side of string</summary> /// <param name="str">the string to trim /// </param> /// <param name="trimNullChars">Whether to remove NULL characters or not /// </param> /** * @param {?} str * @param {?} trimNullChars * @return {?} */ function (str, trimNullChars) { /** @type {?} */ var result; if (typeof str === "undefined" || str === null || str.length === 0) { result = str; } else { /** @type {?} */ var idx = str.length - 1; if (trimNullChars) { while (idx >= 0 && (str[idx] === ' ' || str[idx] === String.fromCharCode(0) /*''*/)) { idx = idx - 1; } } else { while (idx >= 0 && str[idx] === ' ') { idx = idx - 1; } } idx = idx + 1; if (idx < str.length) { result = str.substr(0, idx); } else { result = str; } } return result; }; /// <summary> remove spaces from the left side of string</summary> /// <param name="str">the string to trim /// </param> /// <summary> remove spaces from the left side of string</summary> /// <param name="str">the string to trim /// </param> /** * @param {?} str * @return {?} */ StrUtil.ltrim = /// <summary> remove spaces from the left side of string</summary> /// <param name="str">the string to trim /// </param> /** * @param {?} str * @return {?} */ function (str) { /** @type {?} */ var length = str.length; /** @type {?} */ var i = 0; /** @type {?} */ var result; if (str === null || length === 0) { result = str; } else { while (i < length && str[i] === ' ' /*' '*/) { i = i + 1; } if (i > 0) { str = str.substr(i); } result = str; } return result; }; /// <summary>This function for Deleting String from end & start of input /// String /// </summary> /// <param name="str">String , which can include strToDelete spaces on input /// </param> /// <param name="strToDelete">need delete this String from start/end of str. /// </param> /// <returns> String without strToDelete on end & start, /// or 'null' if Sting hasn't not characters inside /// </returns> /// <summary>This function for Deleting String from end & start of input /// String /// </summary> /// <param name="str">String , which can include strToDelete spaces on input /// </param> /// <param name="strToDelete">need delete this String from start/end of str. /// </param> /// <returns> String without strToDelete on end & start, /// or 'null' if Sting hasn't not characters inside /// </returns> /** * @param {?} str * @param {?} strToDelete * @return {?} */ StrUtil.DeleteStringsFromEnds = /// <summary>This function for Deleting String from end & start of input /// String /// </summary> /// <param name="str">String , which can include strToDelete spaces on input /// </param> /// <param name="strToDelete">need delete this String from start/end of str. /// </param> /// <returns> String without strToDelete on end & start, /// or 'null' if Sting hasn't not characters inside /// </returns> /** * @param {?} str * @param {?} strToDelete * @return {?} */ function (str, strToDelete) { if (str.startsWith(strToDelete)) { str = str.substr(strToDelete.length); } if (str.endsWith(strToDelete)) { str = str.substr(0, str.length - strToDelete.length); } /** @type {?} */ var result; if (str.length === 0) { result = null; } else { result = str; } return result; }; /// <summary> pad a string with trailing spaces up to the given length</summary> /// <param name="str">the string to pad /// </param> /// <param name="len">the expected length after padding /// </param> /// <summary> pad a string with trailing spaces up to the given length</summary> /// <param name="str">the string to pad /// </param> /// <param name="len">the expected length after padding /// </param> /** * @param {?} str * @param {?} len * @return {?} */ StrUtil.padStr = /// <summary> pad a string with trailing spaces up to the given length</summary> /// <param name="str">the string to pad /// </param> /// <param name="len">the expected length after padding /// </param> /** * @param {?} str * @param {?} len * @return {?} */ function (str, len) { /** @type {?} */ var padLen = len - str.length; if (padLen > 0) { if (StrUtil._paddingSpaces === null || StrUtil._paddingSpaces.length < padLen) { StrUtil._paddingSpaces = NString.FromChar(' ', padLen); } /** @type {?} */ var stringBuilder = new StringBuilder(len); stringBuilder.Append(str); stringBuilder.Append(StrUtil._paddingSpaces, 0, padLen); str = stringBuilder.ToString(); } return str; }; /// <summary> this method will serve as a string tokenizer instead of using the c# split method /// since there are diffrences btween java tokenizer and c# split /// the implimentation given by the conversion tool is not Sufficient /// </summary> /// <param name="source">- the source string to be converted /// </param> /// <param name="delim">- the string of delimiters used to split the string (each character in the String is a delimiter /// </param> /// <returns> array of token according which is the same as string tokenizer in java /// </returns> /// <summary> this method will serve as a string tokenizer instead of using the c# split method /// since there are diffrences btween java tokenizer and c# split /// the implimentation given by the conversion tool is not Sufficient /// </summary> /// <param name="source">- the source string to be converted /// </param> /// <param name="delim">- the string of delimiters used to split the string (each character in the String is a delimiter /// </param> /// <returns> array of token according which is the same as string tokenizer in java /// </returns> /** * @param {?} source * @param {?} delim * @return {?} */ StrUtil.tokenize = /// <summary> this method will serve as a string tokenizer instead of using the c# split method /// since there are diffrences btween java tokenizer and c# split /// the implimentation given by the conversion tool is not Sufficient /// </summary> /// <param name="source">- the source string to be converted /// </param> /// <param name="delim">- the string of delimiters used to split the string (each character in the String is a delimiter /// </param> /// <returns> array of token according which is the same as string tokenizer in java /// </returns> /** * @param {?} source * @param {?} delim * @return {?} */ function (source, delim) { // It is mentioned in the comment that we should not use String.Split() // because its behavior is different than Java's tokenizer. // So, we were suppose to use our own implementation (the commented code below). // But all these years, we were calling XmlParser.getToken() which was actually // using String.Split(). And we didn't face any problem. // So, it seems that we do not have problem in using String.Split(). // But now, we can improve the performance here... // XmlParser.getTokens() was getting a String[] using String.Split(). // It was then creating a List<String> from this String[] and was returning it to // tokenize(). // tokenize() was again converting this List<String> back to String[]. // So why not call String.Split() directly? return source.split(delim); /* String [] tokens = null; char [] delimArry = delim.toCharArray(); //since java discards delimiters from the start and end of the string and c# does not //we need to remove them manually // source = source.TrimEnd(delimArry); // source = source.TrimStart(delimArry); source = source.trim(); //now that we have remove starting and ending delimiters we can split tokens = source.Split(delimArry); /* * only one problem: if we have two Subsequent delimiters for example : * the delimiter is ';' and the string is: "first;;second;third" * then in java String tokenizer will give us only 3 tokens :first,second and third * while is c# split wethod will return 4 tokens: first,empty string,second and third * we need to deal with that */ /* List res = new List(); for (int i = 0 ; i < tokens.length; i++) { if (tokens[i] != "" ) res.addItem(tokens[i]); } return (String [])(res.getAllItems (String.class));*/ }; /// <summary> /// translate from string to hexa dump char by char /// </summary> /// <param name = "string">to translate it to the byte stream</param> /// <param name = "minLength">the minimal length of hexa digits for each char</param> /// <returns> the byte stream in form of string</returns> /// <summary> /// translate from string to hexa dump char by char /// </summary> /// <param name = "string">to translate it to the byte stream</param> /// <param name = "minLength">the minimal length of hexa digits for each char</param> /// <returns> the byte stream in form of string</returns> /** * @param {?} str * @param {?} minLength * @return {?} */ StrUtil.stringToHexaDump = /// <summary> /// translate from string to hexa dump char by char /// </summary> /// <param name = "string">to translate it to the byte stream</param> /// <param name = "minLength">the minimal length of hexa digits for each char</param> /// <returns> the byte stream in form of string</returns> /** * @param {?} str * @param {?} minLength * @return {?} */ function (str, minLength) { /** @type {?} */ var stringBuilder = new StringBuilder(str.length * minLength); for (var indx = 0; indx < str.length; indx = indx + 1) { /** @type {?} */ var currInt = str.charCodeAt(indx); /** @type {?} */ var hexStr = currInt.toString(16); while (hexStr.length < minLength) { hexStr = "0" + hexStr; } stringBuilder.Append(hexStr); } return stringBuilder.ToString().toUpperCase(); }; /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ StrUtil.searchAndReplace = /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ function (str, from, to) { if (arguments.length === 3 && (str === null || str.constructor === String) && (from === null || from.constructor === String) && (to === null || to.constructor === String)) { return StrUtil.searchAndReplace_0(str, from, to); } return StrUtil.searchAndReplace_1(str, from, to); }; /// <summary> replace every appearance of 'from' in 'str' with 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /// <summary> replace every appearance of 'from' in 'str' with 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ StrUtil.searchAndReplace_0 = /// <summary> replace every appearance of 'from' in 'str' with 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ function (str, from, to) { /** @type {?} */ var lastSubStr = 0; /** @type {?} */ var startSubStr; /** @type {?} */ var result; if ((startSubStr = str.indexOf(from)) === -1) { result = str; } else { /** @type {?} */ var stringBuilder = new StringBuilder(str.length); while (startSubStr !== -1) { stringBuilder.Append(str.substr(lastSubStr, startSubStr - lastSubStr) + to); startSubStr = startSubStr + from.length; lastSubStr = startSubStr; startSubStr = str.indexOf(from, lastSubStr); } stringBuilder.Append(str.substr(lastSubStr)); result = stringBuilder.ToString(); } return result; }; /// <summary> replace every appearance of strings of 'from' in 'str' with the according string in 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /// <summary> replace every appearance of strings of 'from' in 'str' with the according string in 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ StrUtil.searchAndReplace_1 = /// <summary> replace every appearance of strings of 'from' in 'str' with the according string in 'to'</summary> /// <param name="str">the working base source string </param> /// <param name="from">the string to replace </param> /// <param name="to">the string use instead 'from' </param> /// <returns> modified String </returns> /** * @param {?} str * @param {?} from * @param {?} to * @return {?} */ function (str, from, to) { /** @type {?} */ var lastSubStr = 0; /** @type {?} */ var sarIndex = 0; /** @type {?} */ var fromCopy = from.slice(); /** @type {?} */ var startSubStr; /** @type {?} */ var SARindex = new RefParam(0); startSubStr = StrUtil.indexOf(str, fromCopy, lastSubStr, SARindex); sarIndex = SARindex.value; if (startSubStr === -1) return str; /** @type {?} */ var result; /** @type {?} */ var tmpBuf = new StringBuilder(str.length); while (startSubStr !== -1) { tmpBuf.Append(str.substr(lastSubStr, startSubStr - lastSubStr) + to[sarIndex]); startSubStr += fromCopy[sarIndex].length; lastSubStr = startSubStr; startSubStr = StrUtil.indexOf(str, fromCopy, lastSubStr, SARindex); sarIndex = SARindex.value; } ; tmpBuf.Append(str.substr(lastSubStr)); result = tmpBuf.ToString(); return result; }; /// <summary> this functions is for use by the searchAndReplace() function - /// searches the offset of the strings from the array in the given string /// and returns the minimum offset found and sets the index of the found string /// to SARindex /// </summary> /// <param name="str">the string to search in </param> /// <param name="strings">an array of strings to search for </param> /// <param name="offset">where to start the search </param> /// <summary> this functions is for use by the searchAndReplace() function - /// searches the offset of the strings from the array in the given string /// and returns the minimum offset found and sets the index of the found string /// to SARindex /// </summary> /// <param name="str">the string to search in </param> /// <param name="strings">an array of strings to search for </param> /// <param name="offset">where to start the search </param> /** * @param {?} str * @param {?} strings * @param {?} offset * @param {?} SARindex * @return {?} */ StrUtil.indexOf = /// <summary> this functions is for use by the searchAndReplace() function - /// searches the offset of the strings from the array in the given string /// and returns the minimum offset found and sets the index of the found string /// to SARindex /// </summary> /// <param name="str">the string to search in </param> /// <param name="strings">an array of strings to search for </param> /// <param name="offset">where to start the search </param> /** * @param {?} str * @param {?} strings * @param {?} offset * @param {?} SARindex * @return {?} */ function (str, strings, offset, SARindex) { /** @type {?} */ var minOffset = -1; for (var i = 0; i < strings.length; i = i + 1) { /** @type {?} */ var flag = strings[i] === null; if (!(strings[i] === null)) { /** @type {?} */ var resultOffset = str.indexOf(strings[i], offset); if (resultOffset === -1) { strings[i] = null; } else { if (resultOffset < minOffset || minOffset === -1) { minOffset = resultOffset; SARindex.value = i; } } } } /** @type {?} */ var result; if (minOffset > -1) { result = minOffset; } else { SARindex.value = -1; result = -1; } return result; }; /// <summary> replace tokens in user string by vector values </summary> /// <param name="userString">- user buffer like "User %d, %d string" /// </param> /// <param name="token">- token used in user string - i.e. "%d" /// </param> /// <param name="occurrence">- number of token where replace will take part (1 for first occurrence) /// </param> /// <param name="value">- value to be inserted insted of token /// </param> /// <summary> replace tokens in user string by vector values </summary> /// <param name="userString">- user buffer like "User %d, %d string" /// </param> /// <param name="token">- token used in user string - i.e. "%d" /// </param> /// <param name="occurrence">- number of token where replace will take part (1 for first occurrence) /// </param> /// <param name="value">- value to be inserted insted of token /// </param> /** * @param {?} userString * @param {?} token * @param {?} occurrence * @param {?} val * @return {?} */ StrUtil.replaceStringTokens = /// <summary> replace tokens in user string by vector values </summary> /// <param name="userString">- user buffer like "User %d, %d string" /// </param> /// <param name="token">- token used in user string - i.e. "%d" /// </param> /// <param name="occurrence">- number of token where replace will take part (1 for first occurrence) /// </param> /// <param name="value">- value to be inserted insted of token /// </param> /** * @param {?} userString * @param {?} token * @param {?} occurrence * @param {?} val * @return {?} */ function (userString, token, occurrence, val) { /** @type {?} */ var tokenLen = token.length; /** @type {?} */ var currPosition = 0; /** @type {?} */ var newString = userString; if (val !== null) { /** @type {?} */ var num2 = 0; while (num2 < occurrence && currPosition !== -1) { currPosition = userString.indexOf(token, currPosition + ((num2 === 0) ? 0 : tokenLen)); num2 = num2 + 1; } if (currPosition !== -1) { newString = userString.substr(0, currPosition) + val + userString.substr(currPosition + tokenLen, userString.length - (currPosition + tokenLen)); } } return newString; }; /** * @param {?} source * @param {?} type * @return {?} */ StrUtil.makePrintableTokens = /** * @param {?} source * @param {?} type * @return {?} */ function (source, type) { if (arguments.length === 2 && (source === null || source.constructor === String) && (type === null || type.constructor === Number)) { return StrUtil.makePrintableTokens_0(source, type); } StrUtil.makePrintableTokens_1(source, type); }; /// <summary> /// converts special characters in a token to a printable format /// </summary> /// <param name = "source">a token </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR, SEQ_2_STR </param> /// <returns> token with converted special characters </returns> /// <summary> /// converts special characters in a token to a printable format /// </summary> /// <param name = "source">a token </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR, SEQ_2_STR </param> /// <returns> token with converted special characters </returns> /** * @param {?} source * @param {?} type * @return {?} */ StrUtil.makePrintableTokens_0 = /// <summary> /// converts special characters in a token to a printable format /// </summary> /// <param name = "source">a token </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR, SEQ_2_STR </param> /// <returns> token with converted special characters </returns> /** * @param {?} source * @param {?} type * @return {?} */ function (source, type) { /** @type {?} */ var escStr = [ "\\", "-", "," ]; /** @type {?} */ var escSeq = [ "\\\\", "\\-", "\\," ]; /** @type {?} */ var escHtm = [ HTML_BACKSLASH, HTML_HYPHEN, HTML_COMMA ]; /** @type {?} */ var result; switch (type) { case STR_2_HTML: result = StrUtil.searchAndReplace(source, escStr, escHtm); break; case SEQ_2_HTML: result = StrUtil.searchAndReplace(source, escSeq, escHtm); break; case HTML_2_SEQ: result = StrUtil.searchAndReplace(source, escHtm, escSeq); break; case HTML_2_STR: result = StrUtil.searchAndReplace(source, escHtm, escStr); break; case SEQ_2_STR: result = StrUtil.searchAndReplace(source, escSeq, escStr); break; default: result = source; break; } return result; }; /// <summary> /// converts special characters in a tokens collection to a printable format /// </summary> /// <param name = "source">vector of strings before tokenaizer </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR </param> /// <summary> /// converts special characters in a tokens collection to a printable format /// </summary> /// <param name = "source">vector of strings before tokenaizer </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR </param> /** * @param {?} source * @param {?} type * @return {?} */ StrUtil.makePrintableTokens_1 = /// <summary> /// converts special characters in a tokens collection to a printable format /// </summary> /// <param name = "source">vector of strings before tokenaizer </param> /// <param name = "type">type of conversion: STR_2_HTML, SEQ_2_HTML, HTML_2_SEQ, HTML_2_STR </param> /** * @param {?} source * @param {?} type * @return {?} */ function (source, type) { if (source !== null) { /** @type {?} */ var length_1 = source.length; for (var i = 0; i < length_1; i = i + 1) { /** @type {?} */ var currElm = source.get_Item(i); source.set_Item(i, StrUtil.makePrintableTokens_0(currElm, type)); } } }; /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /** * @param {?} source * @return {?} */ StrUtil.makePrintable = /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /** * @param {?} source * @return {?} */ function (source) { /** @type {?} */ var from = [ "\n", "\r", "'", "\\", "\"", "\0" ]; /** @type {?} */ var to = [ "\\n", "\\r", "\\'", "\\\\", "\\\"", "\\0" ]; return StrUtil.searchAndReplace(source, from, to); }; /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation (simplified version for range error message) /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation (simplified version for range error message) /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /** * @param {?} source * @return {?} */ StrUtil.makePrintable2 = /// <summary> /// change non-printable characters like "new line" and "line feed" to their /// printable representation (simplified version for range error message) /// </summary> /// <param name = "source">is the string with non-printable characters </param> /// <returns> the new string where all the non-printable characters are converted </returns> /** * @param {?} source * @return {?} */ function (source) { /** @type {?} */ var from = [ "\n", "\r", "\0" ]; /** @type {?} */ var to = [ "\\n", "\\r", "\\0" ]; return StrUtil.searchAndReplace(source, from, to); }; /// <summary> /// /// </summary> /// <param name="s"></param> /// <param name="len"></param> /// <returns></returns> /// <summary> /// /// </summary> /// <param name="s"></param> /// <param name="len"></param> /// <returns></returns> /** * @param {?} s * @param {?} len * @return {?} */ StrUtil.ZstringMake = /// <summary> /// /// </summary> /// <param name="s"></param> /// <param name="len"></param> /// <returns></returns> /** * @param {?} s * @param {?} len * @return {?} */ function (s, len) { len = StrUtil.mem_trim(s, len); return s.substr(0, len); }; /// <summary>(public) /// returns plain text from rtf text /// </summary> /// <param name="rtfText">refer to the summary</param> /// <returns>refer to the summary</returns> /// <summary>(public) /// returns plain text from rtf text /// </summary> /// <param name="rtfText">refer to the summary</param> /// <returns>refer to the summary</returns> /** * @param {?} rtfText * @return {?} */ StrUtil.GetPlainTextfromRtf = /// <summary>(public) /// returns plain text from rtf text /// </summary> /// <param name="rtfText">refer to the summary</param> /// <returns>refer to the summary</returns> /** * @param {?} rtfText * @return {?} */ function (rtfText) { if (Rtf.isRtf(rtfText)) { /** @type {?} */ var rtf = new Rtf(); /** @type {?} */ var outputTxt = new StringBuilder(""); rtf.toTxt(rtfText, outputTxt); rtfText = outputTxt.ToString(); } return rtfText; }; /// <summary> /// Returns true if the string arrays str1 & str2 are equal /// </summary> /// <param name="str1"></param> /// <param name="str2"></param> /// <returns></returns> /// <summary> /// Returns true if the string arrays str1 & str2 are equal /// </summary> /// <param name="str1"></param> /// <param name="str2"></param> /// <returns></returns> /** * @param {?} str1 * @param {?} str2 * @return {?} */ StrUtil.StringsArraysEqual = /// <summary> /// Returns true if the string arrays str1 & str2 are equal /// </summary> /// <param name="str1"></param> /// <param name="str2"></param> /// <returns></returns> /** * @param {?} str1 * @param {?} str2 * @return {?} */ function (str1, str2) { /** @type {?} */ var result; if (str1 === null && str2 === null) { result = true; } else { if (str1 === null || str2 === null) { result = false; } else { if (str1.length !== str2.length) { result = false; } else { for (var index = 0; index < (/** @type {?} */ (str1.length)); index = index + 1) { if ((str1[index] !== str2[index])) { result = false; return result; } } result = true; } } } return result; }; /// <summary> /// The code is copied from tsk_open_bnd_wild and SearchAndReplaceWildChars /// The refactoring is not performed for backwards compatibility /// The code replaces special charachters :* ? with recieved filler /// </summary> /// <returns></returns> /// <summary> /// The code is copied from tsk_open_bnd_wild and SearchAndReplaceWildChars /// The refactoring is not performed for backwards compatibility /// The code replaces special charachters :* ? with recieved filler /// </summary> /// <returns></returns> /** * @param {?} buf * @param {?} len * @param {?} filler * @return {?} */ StrUtil.SearchAndReplaceWildChars = /// <summary> /// The code is copied from tsk_open_bnd_wild and SearchAndReplaceWildChars /// The refactoring is not performed for backwards compatibility /// The code replaces special charachters :* ? with recieved filler /// </summary> /// <returns></returns> /** * @param {?} buf * @param {?} len * @param {?} filler * @return {?} */ function (buf, len, filler) { buf = NString.PadRight(buf, len); /** @type {?} */ var escChar = false; /** @type {?} */ var stopSearch = false; /** @type {?} */ var tmpBuf = new StringBuilder(len); for (var i = 0; i < len; i = i + 1) { switch (buf[i]) { case ('\\'): { /** @type {?} */ var isNextCharWild = true; //If next char is not wild , then copy '\', if this is first char. if ((i + 1 < len) && (buf[i + 1] != '*' && buf[i + 1] != '\\')) isNextCharWild = false; if (escChar || !isNextCharWild) tmpBuf.Append(buf[i]); escChar = !escChar; } break; case ('*'): if (escChar) tmpBuf.Append(buf[i]); else { tmpBuf.Append(filler, len - tmpBuf.Length); stopSearch = true; } escChar = false; break; case '?': tmpBuf.Append(filler); escChar = false; break; default: tmpBuf.Append(buf[i]); escChar = false; break; } } /** @type {?} */ var text = tmpBuf.ToString(); return NString.TrimEnd(NString.TrimEnd(text, ['\0'])); }; StrUtil._paddingSpaces = null; return StrUtil; }()); export { StrUtil }; if (false) { /** @type {?} */ StrUtil._paddingSpaces; } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU3RyVXRpbC5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0BtYWdpYy14cGEvdXRpbHMvIiwic291cmNlcyI6WyJzcmMvU3RyVXRpbC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBQUEsT0FBTyxFQUFPLE9BQU8sRUFBRSxRQUFRLEVBQUUsYUFBYSxFQUFDLE1BQU0sc0JBQXNCLENBQUM7QUFDNUUsT0FBTyxFQUFDLGVBQWUsRUFBQyxNQUFNLG1CQUFtQixDQUFDO0FBQ2xELE9BQU8sRUFBQyxHQUFHLEVBQUMsTUFBTSxPQUFPLENBQUM7O0lBRXBCLGNBQWMsR0FBVyxRQUFROztJQUNqQyxVQUFVLEdBQVcsUUFBUTs7SUFDN0IsV0FBVyxHQUFXLFFBQVE7O0lBQzlCLFVBQVUsR0FBVyxDQUFDOztBQUM1QixNQUFNLEtBQU8sVUFBVSxHQUFXLENBQUM7O0FBQ25DLE1BQU0sS0FBTyxVQUFVLEdBQVcsQ0FBQzs7SUFDN0IsVUFBVSxHQUFXLENBQUM7O0FBQzVCLE1BQU0sS0FBTyxTQUFTLEdBQVcsQ0FBQztBQUVsQztJQUFBO0lBeXBCQSxDQUFDO0lBdHBCQyxrREFBa0Q7Ozs7Ozs7SUFDM0MsZ0JBQVE7Ozs7Ozs7SUFBZixVQUFnQixHQUFXLEVBQUUsR0FBVzs7WUFDbEMsTUFBYztRQUNsQixFQUFFLENBQUMsQ0FBQyxHQUFHLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQztZQUNaLEVBQUUsQ0FBQyxDQUFDLEdBQUcsR0FBRyxHQUFHLENBQUMsTUFBTSxDQUFDLENBQUMsQ0FBQztnQkFDckIsTUFBTSxHQUFHLENBQUMsQ0FBQyxDQUFDO2dCQUNaLE1BQU0sQ0FBQyxNQUFNLENBQUM7WUFDaEIsQ0FBQztZQUNELE9BQU8sR0FBRyxHQUFHLENBQUMsSUFBSSxHQUFHLENBQUMsR0FBRyxHQUFHLENBQUMsQ0FBQyxLQUFLLEdBQUcsRUFBRSxDQUFDO2dCQUN2QyxHQUFHLEdBQUcsR0FBRyxHQUFHLENBQU