| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma rtFunctionErrors=1 |
| | 3 | | #pragma version=1.10 |
| | 4 | | #pragma TextEncoding="UTF-8" |
| | 5 | | #pragma ModuleName=IUTF_Utils_Strings |
| | 6 | |
|
| | 7 | | // This pattern is used to find all placeholder in the string. This pattern consists of multiple parts: |
| | 8 | | // #1: ^#2#(?:3#5)?$ |
| | 9 | | // The full pattern. |
| | 10 | | // #2: (.*?) |
| | 11 | | // The prefix in front of the placeholder |
| | 12 | | // #3: (@?#6#4) |
| | 13 | | // The pattern for our placeholder including wave placeholder extension |
| | 14 | | // #4: %[^a-zA-Z%]*(?:[feEgGdousxXbc]|W\dP) |
| | 15 | | // The pattern for placeholder that are provided to printf |
| | 16 | | // #5: (.*) |
| | 17 | | // The pattern after the placeholder |
| | 18 | | // #6: (?<!%) |
| | 19 | | // At this position is no % sign allowed and do not consume this char. This is used to prevent %% matching. |
| | 20 | | static StrConstant USER_PRINT_PATTERN = "^(.*?)(?:(@?(?<!%)%[^a-zA-Z%]*(?:[feEgGdousxXbc]|W\\dP))(.*))?$" |
| | 21 | |
|
| | 22 | | /// @brief Formats the provided string with the arguments using printf. This allows a higher flexibility |
| | 23 | | /// as such as it supports are flexible number of arguments and custom extensions. @n |
| | 24 | | /// It is required to use all provided arguments in the format string. @n |
| | 25 | | /// If for some reasons the conversion failed the parameter err is set different to 0 and the error message is |
| | 26 | | /// returned. |
| | 27 | | /// @param format The format string with the parameter placeholder |
| | 28 | | /// @param strings The wave of strings that are used for the placeholder |
| | 29 | | /// @param numbers The wave of numbers that are used for the placeholder |
| | 30 | | /// @param[out] err Set different to 0 if an error during conversion happens. Set to 0 if the conversion succeeds. |
| | 31 | | /// @returns The formated message if succeeds. The error message if failed. |
| 195 | 32 | | static Function/S UserPrintF(format, strings, numbers, err) |
| | 33 | | string format |
| | 34 | | WAVE/T strings |
| | 35 | | WAVE numbers |
| | 36 | | variable &err |
| | 37 | |
|
| 195 | 38 | | string part1, part2, part3, str |
| 195 | 39 | | variable num, i, stringIndex, numberIndex |
| 195 | 40 | | variable stringLength = DimSize(strings, UTF_ROW) |
| 195 | 41 | | variable numberLength = DimSize(numbers, UTF_ROW) |
| 195 | 42 | | string result = "" |
| | 43 | |
|
| 195 | 44 | | err = 0 |
| | 45 | |
|
| 195 | 46 | | if(GetRTError(0)) |
| 0 | 47 | | sprintf result, "Pending RTE detected: \"%s\" in \"%s\"", GetRTErrMessage(), GetRTStackInfo(3) |
| 0 | 48 | | err = 1 |
| 0 | 49 | | return result |
| 195 | 50 | | endif |
| | 51 | |
|
| 195 | 52 | | for(; !IUTF_Utils#IsEmpty(format);) |
| 225 | 53 | | SplitString/E=USER_PRINT_PATTERN format, part1, part2, part3 |
| 225 | 54 | | num = V_flag |
| | 55 | |
|
| 225 | 56 | | if(num < 1) |
| 0 | 57 | | break |
| 225 | 58 | | endif |
| | 59 | |
|
| 225 | 60 | | sprintf str, part1; err = GetRTError(1) |
| 225 | 61 | | if(err) |
| 0 | 62 | | sprintf str, "PrintF failed: format=\"%s\", err=%d", part1, err |
| 0 | 63 | | return str |
| 225 | 64 | | endif |
| 225 | 65 | | result += str |
| | 66 | |
|
| 225 | 67 | | if(num < 2) |
| 195 | 68 | | break |
| 30 | 69 | | endif |
| | 70 | |
|
| 30 | 71 | | if(!CmpStr(part2[0], "@")) |
| 0 | 72 | | part2 = part2[1, Inf] |
| 0 | 73 | | if(!CmpStr(part2, "%s")) |
| | 74 | |
|
| 0 | 75 | | if(stringIndex > 0) |
| 0 | 76 | | err = 1 |
| 0 | 77 | | return "Cannot use full string wave if single entries are used" |
| 0 | 78 | | endif |
| | 79 | |
|
| 0 | 80 | | result += "{" |
| 0 | 81 | | for(i = 0; i < stringLength; i += 1) |
| 0 | 82 | | if(i > 0) |
| 0 | 83 | | result += ", " |
| 0 | 84 | | endif |
| 0 | 85 | | result += strings[i] |
| 0 | 86 | | endfor |
| 0 | 87 | | result += "}" |
| | 88 | |
|
| 0 | 89 | | stringIndex = stringLength |
| | 90 | |
|
| 0 | 91 | | else |
| | 92 | |
|
| 0 | 93 | | if(numberIndex > 0) |
| 0 | 94 | | err = 1 |
| 0 | 95 | | return "Cannot use full number wave if single entries are used" |
| 0 | 96 | | endif |
| | 97 | |
|
| 0 | 98 | | result += "{" |
| 0 | 99 | | for(i = 0; i < numberLength; i += 1) |
| 0 | 100 | | if(i > 0) |
| 0 | 101 | | result += ", " |
| 0 | 102 | | endif |
| | 103 | |
|
| 0 | 104 | | sprintf str, part2, numbers[i]; err = GetRTError(1) |
| 0 | 105 | | if(err) |
| 0 | 106 | | sprintf str, "Cannot format number %d (%g) using format \"%s\"", i, numbers[i], part2 |
| 0 | 107 | | return str |
| 0 | 108 | | endif |
| 0 | 109 | | result += str |
| 0 | 110 | | endfor |
| 0 | 111 | | result += "}" |
| | 112 | |
|
| 0 | 113 | | numberIndex = numberLength |
| | 114 | |
|
| 0 | 115 | | endif |
| 30 | 116 | | else |
| 30 | 117 | | if(!CmpStr(part2, "%s")) |
| | 118 | |
|
| 30 | 119 | | if(stringIndex >= stringLength) |
| 0 | 120 | | err = 1 |
| 0 | 121 | | sprintf str, "Cannot load string %d, only %d are provided", stringIndex, stringLength |
| 0 | 122 | | return str |
| 30 | 123 | | endif |
| | 124 | |
|
| 30 | 125 | | result += strings[stringIndex] |
| 30 | 126 | | stringIndex += 1 |
| | 127 | |
|
| 0 | 128 | | else |
| | 129 | |
|
| 0 | 130 | | if(numberIndex >= numberLength) |
| 0 | 131 | | err = 1 |
| 0 | 132 | | sprintf str, "Cannot load number %d, only %d are provided", numberIndex, numberLength |
| 0 | 133 | | return str |
| 0 | 134 | | endif |
| | 135 | |
|
| 0 | 136 | | sprintf str, part2, numbers[numberIndex]; err = GetRTError(1) |
| 0 | 137 | | if(err) |
| 0 | 138 | | sprintf str, "Cannot format number %d (%g) using format \"%s\"", numberIndex, numbers[numberIndex], part2 |
| 0 | 139 | | return str |
| 0 | 140 | | endif |
| | 141 | |
|
| 0 | 142 | | result += str |
| 0 | 143 | | numberIndex += 1 |
| | 144 | |
|
| 30 | 145 | | endif |
| 30 | 146 | | endif |
| | 147 | |
|
| 30 | 148 | | format = part3 |
| | 149 | |
|
| 30 | 150 | | endfor |
| | 151 | |
|
| 195 | 152 | | if(stringIndex < stringLength) |
| 0 | 153 | | err = 1 |
| 0 | 154 | | sprintf str, "Only %d out of %d strings are consumed", stringIndex, stringLength |
| 0 | 155 | | return str |
| 195 | 156 | | endif |
| 195 | 157 | | if(numberIndex < numberLength) |
| 0 | 158 | | err = 1 |
| 0 | 159 | | sprintf str, "Only %d out of %d numbers are consumed", numberIndex, numberLength |
| 0 | 160 | | return str |
| 195 | 161 | | endif |
| | 162 | |
|
| 195 | 163 | | return result |
| 195 | 164 | | End |
| | 165 | |
|
| | 166 | | /// @brief Shuffles the contents of a list with the best PRNG for the current Igor version. |
| | 167 | | /// |
| | 168 | | /// @param list The list that is requested to be shuffled |
| | 169 | | /// @param separator (optional, default ";") the separator of the list elements |
| | 170 | | /// |
| | 171 | | /// @returns The shuffled list |
| 0 | 172 | | static Function/S ShuffleList(list, [separator]) |
| | 173 | | string list, separator |
| | 174 | |
|
| 0 | 175 | | string result |
| | 176 | |
|
| 0 | 177 | | separator = SelectString(ParamIsDefault(separator), separator, ";") |
| | 178 | |
|
| 0 | 179 | | WAVE/T wv = ListToTextWave(list, separator) |
| 0 | 180 | | IUTF_Utils_Waves#InPlaceShuffleText1D(wv) |
| 0 | 181 | | wfprintf result, "%s" + separator, wv |
| | 182 | |
|
| 0 | 183 | | return result |
| 0 | 184 | | End |
| | 185 | |
|
| | 186 | | /// Returns 1 if text starts with prefix, 0 otherwise. |
| 45 | 187 | | static Function IsPrefix(text, prefix) |
| | 188 | | string text, prefix |
| | 189 | |
|
| 45 | 190 | | variable textLength = strlen(text) |
| 45 | 191 | | variable prefixLength = strlen(prefix) |
| | 192 | |
|
| 45 | 193 | | if(textLength < prefixLength) |
| 0 | 194 | | return 0 |
| 45 | 195 | | endif |
| | 196 | |
|
| 45 | 197 | | return !CmpStr(text[0, prefixLength - 1], prefix) |
| 45 | 198 | | End |
| | 199 | |
|
| | 200 | | /// @brief Search for all matches of the specified regex in the text and replaces it with the |
| | 201 | | /// replacement |
| | 202 | | /// |
| | 203 | | /// @param regex The regular string that is used to find the text to replace. This string is |
| | 204 | | /// not allowed to contain capture groups |
| | 205 | | /// @param text The text to search in |
| | 206 | | /// @param replacement The text that should be placed for all matches |
| | 207 | | /// |
| | 208 | | /// @return The new text with all matches replaced |
| 660 | 209 | | static Function/S ReplaceAllRegex(regex, text, replacement) |
| | 210 | | string regex, text, replacement |
| | 211 | |
|
| 660 | 212 | | string part1, part2, part3 |
| 660 | 213 | | string result = "" |
| | 214 | |
|
| 660 | 215 | | for(; !IUTF_Utils#IsEmpty(text);) |
| 660 | 216 | | SplitString/E=("^(.*?)(" + regex + ")(.*)$") text, part1, part2, part3 |
| | 217 | |
|
| 660 | 218 | | if(!V_flag) |
| 585 | 219 | | return result + text |
| 75 | 220 | | endif |
| | 221 | |
|
| 75 | 222 | | result += part1 + replacement |
| 75 | 223 | | text = part3 |
| 75 | 224 | | endfor |
| | 225 | |
|
| 75 | 226 | | return result |
| 660 | 227 | | End |
| | 228 | |
|
| | 229 | | /// @brief Count the number of matches of a regex in the specified text. |
| | 230 | | /// |
| | 231 | | /// @param regex The regular string that is used to count matches. This string is not allowed |
| | 232 | | /// to contain capture groups |
| | 233 | | /// @param text The text to search in |
| | 234 | | /// |
| | 235 | | /// @return The number of matches |
| 240 | 236 | | static Function CountRegex(regex, text) |
| | 237 | | string regex, text |
| | 238 | |
|
| 240 | 239 | | string part1, part2, part3 |
| 240 | 240 | | variable count |
| | 241 | |
|
| 240 | 242 | | for(; !IUTF_Utils#IsEmpty(text);) |
| 365 | 243 | | SplitString/E=("^(.*?)(" + regex + ")(.*)$") text, part1, part2, part3 |
| | 244 | |
|
| 365 | 245 | | if(!V_flag) |
| 190 | 246 | | return count |
| 175 | 247 | | endif |
| | 248 | |
|
| 175 | 249 | | count += 1 |
| 175 | 250 | | text = part3 |
| 175 | 251 | | endfor |
| | 252 | |
|
| 50 | 253 | | return count |
| 240 | 254 | | End |