| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma rtFunctionErrors=1 |
| | 3 | | #pragma version=1.10 |
| | 4 | | #pragma TextEncoding="UTF-8" |
| | 5 | | #pragma ModuleName=IUTF_Utils |
| | 6 | |
|
| | 7 | | static Constant UTF_MAXDIFFCOUNT = 10 |
| | 8 | |
|
| | 9 | | ///@cond HIDDEN_SYMBOL |
| | 10 | |
|
| | 11 | | // Choosen so that we don't hit the IP6 limit of 1000 chars |
| | 12 | | // with two %s and some other text. |
| | 13 | | static Constant MAX_STRING_LENGTH = 250 |
| | 14 | |
|
| | 15 | | // The number of significant decimal digits a fp32 or fp64 can hold. This value depends on the |
| | 16 | | // length of the mantisse (23 bits for fp32 and 53 bits for fp64) and are calculated using this |
| | 17 | | // formula: mantisse * log(2). The values are rounded up to the next closest integer. |
| | 18 | | static Constant UTF_DECIMAL_DIGITS_FP32 = 7 |
| | 19 | | static Constant UTF_DECIMAL_DIGITS_FP64 = 16 |
| | 20 | |
|
| | 21 | | // The range of printable ASCII characters. |
| | 22 | | static Constant ASCII_PRINTABLE_START = 32 |
| | 23 | | static Constant ASCII_PRINTABLE_END = 126 |
| | 24 | |
|
| | 25 | | // The maximum amount of bytes that should be printed as context before the difference in |
| | 26 | | // a string diff |
| | 27 | | static Constant MAX_STRING_DIFF_CONTEXT = 10 |
| | 28 | |
|
| | 29 | | // The comparison mode to use for CmpStr. This is binary for Igor >= 7.05 and case sensitive |
| | 30 | | // text comparison for older versions. |
| | 31 | | #if IgorVersion() >= 7.05 |
| | 32 | | static Constant UTF_CMPSTR_MODE = 2 |
| | 33 | | #else |
| | 34 | | static Constant UTF_CMPSTR_MODE = 1 |
| | 35 | | #endif |
| | 36 | |
|
| | 37 | | // The result of a string diff |
| | 38 | | Structure IUTF_StringDiffResult |
| | 39 | | string v1 |
| | 40 | | string v2 |
| | 41 | | EndStructure |
| | 42 | |
|
| | 43 | | /// @brief Returns 1 if var is a finite/normal number, 0 otherwise |
| | 44 | | /// |
| | 45 | | /// @hidecallgraph |
| | 46 | | /// @hidecallergraph |
| 5 | 47 | | threadsafe static Function IsFinite(var) |
| | 48 | | variable var |
| | 49 | |
|
| 5 | 50 | | return numType(var) == 0 |
| 5 | 51 | | End |
| | 52 | |
|
| | 53 | | /// @brief Returns 1 if var is a NaN, 0 otherwise |
| | 54 | | /// |
| | 55 | | /// @hidecallgraph |
| | 56 | | /// @hidecallergraph |
| 45 | 57 | | threadsafe static Function IsNaN(var) |
| | 58 | | variable var |
| | 59 | |
|
| 45 | 60 | | return numType(var) == 2 |
| 45 | 61 | | End |
| | 62 | |
|
| | 63 | | /// @brief Returns 1 if str is null, 0 otherwise |
| | 64 | | /// @param str must not be a SVAR |
| | 65 | | /// |
| | 66 | | /// @hidecallgraph |
| | 67 | | /// @hidecallergraph |
| 5 | 68 | | threadsafe static Function IsNull(str) |
| | 69 | | string &str |
| | 70 | |
|
| 5 | 71 | | variable len = strlen(str) |
| 5 | 72 | | return numtype(len) == 2 |
| 5 | 73 | | End |
| | 74 | |
|
| | 75 | | /// @brief Returns one if str is empty, zero otherwise. |
| | 76 | | /// @param str any non-null string variable or text wave element |
| | 77 | | /// |
| | 78 | | /// @hidecallgraph |
| | 79 | | /// @hidecallergraph |
| 2615 | 80 | | threadsafe static Function IsEmpty(str) |
| | 81 | | string str |
| | 82 | |
|
| 2615 | 83 | | return !(strlen(str) > 0) |
| 2615 | 84 | | End |
| | 85 | |
|
| | 86 | | /// @brief Returns one if var is a nonfinite integer, zero otherwise |
| 5 | 87 | | threadsafe static Function IsInteger(var) |
| | 88 | | variable var |
| | 89 | |
|
| 5 | 90 | | return trunc(var) == var && numtype(var) == 0 |
| 5 | 91 | | End |
| | 92 | |
|
| | 93 | | /// @brief Convert a text wave to string list |
| | 94 | | /// |
| | 95 | | /// @param[in] txtWave 1D text wave |
| | 96 | | /// @param[in] sep separator string |
| | 97 | | /// @returns string with list of former text wave elements |
| 27 | 98 | | static Function/S TextWaveToList(txtWave, sep) |
| | 99 | | WAVE/T txtWave |
| | 100 | | string sep |
| | 101 | |
|
| 27 | 102 | | string list = "" |
| 27 | 103 | | variable i, numRows |
| | 104 | |
|
| 27 | 105 | | numRows = DimSize(txtWave, 0) |
| 27 | 106 | | for(i = 0; i < numRows; i += 1) |
| 79 | 107 | | list = AddListItem(txtWave[i], list, sep, Inf) |
| 79 | 108 | | endfor |
| | 109 | |
|
| 27 | 110 | | return list |
| 27 | 111 | | End |
| | 112 | |
|
| | 113 | | /// @brief Prepare the passed string for output |
| | 114 | | /// |
| | 115 | | /// We return a fixed string if it is null and limit its size so that it can be used in a sprintf statement using plain |
| | 116 | | /// "%s". We also do that for IP9, where this limit does not exist anymore, to have a consistent output across all IP |
| | 117 | | /// versions. |
| 5 | 118 | | threadsafe Function/S IUTF_PrepareStringForOut(str, [maxLen]) |
| | 119 | | string &str |
| | 120 | | variable maxLen |
| | 121 | |
|
| 5 | 122 | | variable length |
| 5 | 123 | | string suffix |
| | 124 | |
|
| 5 | 125 | | if(IsNull(str)) |
| 0 | 126 | | return "(null)" |
| 5 | 127 | | endif |
| | 128 | |
|
| 5 | 129 | | maxLen = ParamIsDefault(maxLen) ? MAX_STRING_LENGTH : maxLen |
| | 130 | |
|
| 5 | 131 | | length = strlen(str) |
| | 132 | |
|
| 5 | 133 | | if(length < maxLen) |
| 5 | 134 | | return str |
| 0 | 135 | | endif |
| | 136 | |
|
| 0 | 137 | | suffix = ".." |
| 0 | 138 | | return str[0, maxLen - 1 - strlen(suffix)] + suffix |
| 5 | 139 | | End |
| | 140 | |
|
| | 141 | | ///@endcond // HIDDEN_SYMBOL |
| | 142 | |
|
| | 143 | | /// @brief Returns 1 if all wave elements in wave reference wave wv are of type subType, 0 otherwise |
| 0 | 144 | | static Function HasConstantWaveTypes(wv, subType) |
| | 145 | | WAVE/WAVE wv |
| | 146 | | variable subType |
| | 147 | |
|
| 0 | 148 | | Make/FREE/N=(DimSize(wv, UTF_ROW)) matches |
| 0 | 149 | | MultiThread matches = WaveType(wv[p], 1) == subType |
| | 150 | |
|
| 0 | 151 | | FindValue/V=(0) matches |
| 0 | 152 | | return V_Value == -1 |
| 0 | 153 | | End |
| | 154 | |
|
| 0 | 155 | | static Function/S GetTypeStrFromWaveType(wv) |
| | 156 | | WAVE wv |
| | 157 | |
|
| 0 | 158 | | string str = "" |
| 0 | 159 | | variable type1, type0 |
| | 160 | |
|
| 0 | 161 | | type0 = WaveType(wv) |
| 0 | 162 | | if(type0) |
| 0 | 163 | | if(type0 & IUTF_WAVETYPE0_CMPL) |
| 0 | 164 | | str += "complex " |
| 0 | 165 | | endif |
| 0 | 166 | | if(type0 & IUTF_WAVETYPE0_USGN) |
| 0 | 167 | | str += "unsigned " |
| 0 | 168 | | endif |
| 0 | 169 | | if(type0 & IUTF_WAVETYPE0_FP32) |
| 0 | 170 | | str += "32-bit float" |
| 0 | 171 | | endif |
| 0 | 172 | | if(type0 & IUTF_WAVETYPE0_FP64) |
| 0 | 173 | | str += "64-bit float" |
| 0 | 174 | | endif |
| 0 | 175 | | if(type0 & IUTF_WAVETYPE0_INT8) |
| 0 | 176 | | str += "8-bit integer" |
| 0 | 177 | | endif |
| 0 | 178 | | if(type0 & IUTF_WAVETYPE0_INT16) |
| 0 | 179 | | str += "16-bit integer" |
| 0 | 180 | | endif |
| 0 | 181 | | if(type0 & IUTF_WAVETYPE0_INT32) |
| 0 | 182 | | str += "32-bit integer" |
| 0 | 183 | | endif |
| 0 | 184 | | if(type0 & IUTF_WAVETYPE0_INT64) |
| 0 | 185 | | str += "64-bit integer" |
| 0 | 186 | | endif |
| 0 | 187 | | return str |
| 0 | 188 | | else |
| 0 | 189 | | type1 = WaveType(wv, 1) |
| | 190 | |
|
| 0 | 191 | | if(type1 == IUTF_WAVETYPE1_NULL) |
| 0 | 192 | | return "null wave" |
| 0 | 193 | | endif |
| 0 | 194 | | if(type1 == IUTF_WAVETYPE1_TEXT) |
| 0 | 195 | | return "text wave" |
| 0 | 196 | | endif |
| 0 | 197 | | if(type1 == IUTF_WAVETYPE1_DFR) |
| 0 | 198 | | return "DFREF wave" |
| 0 | 199 | | endif |
| 0 | 200 | | if(type1 == IUTF_WAVETYPE1_WREF) |
| 0 | 201 | | return "WAVE REF wave" |
| 0 | 202 | | endif |
| 0 | 203 | | endif |
| | 204 | |
|
| 0 | 205 | | return "UNKNOWN wave type" |
| 0 | 206 | | End |
| | 207 | |
|
| | 208 | | /// This function assumes that EqualWaves(wv1, wv2, WAVE_DATA, tol) != 1. |
| 0 | 209 | | static Function/S DetermineWaveDataDifference(wv1, wv2, tol) |
| | 210 | | WAVE/Z wv1, wv2 |
| | 211 | | variable tol |
| | 212 | |
|
| 0 | 213 | | string msg |
| 0 | 214 | | variable isComplex1, isComplex2 |
| 0 | 215 | | string wvId1, wvId2 |
| 0 | 216 | | string wvNamePrefix, tmpStr1, tmpStr2 |
| | 217 | |
|
| 0 | 218 | | // Generate names for reference |
| 0 | 219 | | wvId1 = GetWaveNameInDFStr(wv1) |
| 0 | 220 | | wvId2 = GetWaveNameInDFStr(wv2) |
| 0 | 221 | | sprintf wvNamePrefix, "Wave1: %s\rWave2: %s\r", wvId1, wvId2 |
| | 222 | |
|
| 0 | 223 | | // Size Check |
| 0 | 224 | | Make/FREE/D wv1Dims = {DimSize(wv1, UTF_ROW), DimSize(wv1, UTF_COLUMN), DimSize(wv1, UTF_LAYER), DimSize(wv1, UTF_CHUN |
| 0 | 225 | | Make/FREE/D wv2Dims = {DimSize(wv2, UTF_ROW), DimSize(wv2, UTF_COLUMN), DimSize(wv2, UTF_LAYER), DimSize(wv2, UTF_CHUN |
| 0 | 226 | | if(!EqualWaves(wv1Dims, wv2Dims, WAVE_DATA, 0)) |
| 0 | 227 | | Make/FREE/T/N=(2, 1) table |
| 0 | 228 | | wfprintf tmpStr1, "[%d]", wv1Dims |
| 0 | 229 | | wfprintf tmpStr2, "[%d]", wv2Dims |
| 0 | 230 | | table[0][0] = tmpStr1 |
| 0 | 231 | | table[1][0] = tmpStr2 |
| 0 | 232 | | msg = NicifyTableText(table, "Dimension Sizes;") |
| 0 | 233 | | sprintf msg, "Waves differ in dimension sizes\r%s%s", wvNamePrefix, msg |
| 0 | 234 | | return msg |
| 0 | 235 | | endif |
| | 236 | |
|
| 0 | 237 | | // complex type |
| 0 | 238 | | isComplex1 = WaveType(wv1) & IUTF_WAVETYPE0_CMPL |
| 0 | 239 | | isComplex2 = WaveType(wv2) & IUTF_WAVETYPE0_CMPL |
| 0 | 240 | | if(isComplex1 != isComplex2) |
| 0 | 241 | | Make/FREE/T/N=(2, 1) table |
| 0 | 242 | | table[0][0] = GetTypeStrFromWaveType(wv1) |
| 0 | 243 | | table[1][0] = GetTypeStrFromWaveType(wv2) |
| 0 | 244 | | msg = NicifyTableText(table, "Wave Types;") |
| 0 | 245 | | sprintf msg, "Waves differ in complex number type\r%s%s", wvNamePrefix, msg |
| 0 | 246 | | return msg |
| 0 | 247 | | endif |
| | 248 | |
|
| 0 | 249 | | // Data Check |
| 0 | 250 | | Make/FREE/T/N=(2 * UTF_MAXDIFFCOUNT, 3) table |
| 0 | 251 | | SetDimLabel UTF_COLUMN, 0, DIMS, table |
| 0 | 252 | | SetDimLabel UTF_COLUMN, 1, DIMLABEL, table |
| 0 | 253 | | SetDimLabel UTF_COLUMN, 2, ELEMENT, table |
| 0 | 254 | | IterateOverWaves(table, wv1, wv2, wv1Dims[0], wv1Dims[1], wv1Dims[2], wv1Dims[3], tol) |
| 0 | 255 | | if(WaveType(wv1, 1) & IUTF_WAVETYPE1_TEXT) |
| 0 | 256 | | msg = NicifyTableText(table, "Dimensions;Labels;Text;") |
| 0 | 257 | | return "Text waves difference:\r" + wvNamePrefix + msg |
| 0 | 258 | | else |
| 0 | 259 | | msg = NicifyTableText(table, "Dimensions;Labels;Value;") |
| 0 | 260 | | if(tol != 0) |
| 0 | 261 | | sprintf tmpStr1, "Waves difference (tolerance limit %15f):\r", tol |
| 0 | 262 | | else |
| 0 | 263 | | tmpStr1 = "Waves difference:\r" |
| 0 | 264 | | endif |
| 0 | 265 | | return tmpStr1 + wvNamePrefix + msg |
| 0 | 266 | | endif |
| | 267 | |
|
| 0 | 268 | | return "" |
| 0 | 269 | | End |
| | 270 | |
|
| 0 | 271 | | static Function IterateOverWaves(table, wv1, wv2, rows, cols, layers, chunks, tol) |
| | 272 | | WAVE/T table |
| | 273 | | WAVE wv1, wv2 |
| | 274 | | variable rows, cols, layers, chunks, tol |
| | 275 | |
|
| 0 | 276 | | variable i, j, k, l |
| 0 | 277 | | variable locCount |
| 0 | 278 | | variable runTol |
| | 279 | |
|
| 0 | 280 | | if(chunks) |
| 0 | 281 | | for(l = 0; l < chunks; l += 1) |
| 0 | 282 | | for(k = 0; k < layers; k += 1) |
| 0 | 283 | | for(j = 0; j < cols; j += 1) |
| 0 | 284 | | for(i = 0; i < rows; i += 1) |
| 0 | 285 | | AddValueDiffImpl(table, wv1, wv2, locCount, i, j, k, l, 4, runTol, tol) |
| 0 | 286 | | if(locCount == UTF_MAXDIFFCOUNT) |
| 0 | 287 | | return NaN |
| 0 | 288 | | endif |
| 0 | 289 | | endfor |
| 0 | 290 | | endfor |
| 0 | 291 | | endfor |
| 0 | 292 | | endfor |
| 0 | 293 | | elseif(layers) |
| 0 | 294 | | for(k = 0; k < layers; k += 1) |
| 0 | 295 | | for(j = 0; j < cols; j += 1) |
| 0 | 296 | | for(i = 0; i < rows; i += 1) |
| 0 | 297 | | AddValueDiffImpl(table, wv1, wv2, locCount, i, j, k, l, 3, runTol, tol) |
| 0 | 298 | | if(locCount == UTF_MAXDIFFCOUNT) |
| 0 | 299 | | return NaN |
| 0 | 300 | | endif |
| 0 | 301 | | endfor |
| 0 | 302 | | endfor |
| 0 | 303 | | endfor |
| 0 | 304 | | elseif(cols) |
| 0 | 305 | | for(j = 0; j < cols; j += 1) |
| 0 | 306 | | for(i = 0; i < rows; i += 1) |
| 0 | 307 | | AddValueDiffImpl(table, wv1, wv2, locCount, i, j, k, l, 2, runTol, tol) |
| 0 | 308 | | if(locCount == UTF_MAXDIFFCOUNT) |
| 0 | 309 | | return NaN |
| 0 | 310 | | endif |
| 0 | 311 | | endfor |
| 0 | 312 | | endfor |
| 0 | 313 | | else |
| 0 | 314 | | for(i = 0; i < rows; i += 1) |
| 0 | 315 | | AddValueDiffImpl(table, wv1, wv2, locCount, i, j, k, l, 1, runTol, tol) |
| 0 | 316 | | if(locCount == UTF_MAXDIFFCOUNT) |
| 0 | 317 | | return NaN |
| 0 | 318 | | endif |
| 0 | 319 | | endfor |
| 0 | 320 | | endif |
| | 321 | |
|
| 0 | 322 | | Redimension/N=(2 * locCount, -1) table |
| 0 | 323 | | return NaN |
| 0 | 324 | | End |
| | 325 | |
|
| 0 | 326 | | threadsafe static Function/S GetWavePointer_Impl(wv) |
| | 327 | | WAVE wv |
| | 328 | |
|
| 0 | 329 | | variable err |
| 0 | 330 | | string str |
| | 331 | |
|
| 0 | 332 | | Make/FREE/WAVE refWave = {wv} |
| | 333 | |
|
| 0 | 334 | | WAVE ref = refWave |
| | 335 | |
|
| | 336 | | #if IgorVersion() >= 7.0 |
| 0 | 337 | | sprintf str, "%#08x", ref[0]; err = GetRTError(1) |
| | 338 | | #else |
| 0 | 339 | | sprintf str, "%#08x", ref[0] |
| | 340 | | #endif |
| | 341 | |
|
| 0 | 342 | | return str |
| 0 | 343 | | End |
| | 344 | |
|
| | 345 | | /// @brief Return the memory address of the passed wave |
| | 346 | | /// |
| | 347 | | /// Works on all Igor Pro versions without clearing lingering runtime errors. |
| | 348 | | /// The idea is that clearing RTE's in preemptive threads does not modify the |
| | 349 | | /// main thread's one. |
| 0 | 350 | | threadsafe static Function/S GetWavePointer(wv) |
| | 351 | | WAVE wv |
| | 352 | |
|
| | 353 | | #if IgorVersion() >= 7.0 |
| | 354 | |
|
| 0 | 355 | | Make/FREE/T/N=2 address |
| 0 | 356 | | MultiThread address = GetWavePointer_Impl(wv) |
| | 357 | |
|
| 0 | 358 | | return address[0] |
| | 359 | | #else |
| 0 | 360 | | return GetWavePointer_Impl(wv) |
| | 361 | | #endif |
| | 362 | |
|
| 0 | 363 | | End |
| | 364 | |
|
| 0 | 365 | | static Function/S GetWaveNameInDFStr(w) |
| | 366 | | WAVE/Z w |
| | 367 | |
|
| 0 | 368 | | string str |
| | 369 | |
|
| 0 | 370 | | if(!WaveExists(w)) |
| 0 | 371 | | return "_null_" |
| 0 | 372 | | endif |
| | 373 | |
|
| 0 | 374 | | str = NameOfWave(w) |
| 0 | 375 | | if(WaveType(w, 2) != IUTF_WAVETYPE2_FREE) |
| 0 | 376 | | str += " in " + GetWavesDataFolder(w, 1) |
| 0 | 377 | | else |
| 0 | 378 | | str += " (" + GetWavePointer(w) + ")" |
| 0 | 379 | | endif |
| | 380 | |
|
| 0 | 381 | | return str |
| 0 | 382 | | End |
| | 383 | |
|
| 0 | 384 | | static Function ValueEqualInclNaN(v1, v2) |
| | 385 | | variable v1, v2 |
| | 386 | |
|
| 0 | 387 | | variable isNaN1, isNaN2, isInf1, isInf2, isnegInf1, isnegInf2 |
| | 388 | |
|
| 0 | 389 | | isNaN1 = IsNaN(v1) |
| 0 | 390 | | isNaN2 = IsNaN(v2) |
| 0 | 391 | | if(isNaN1 && isNaN2) |
| 0 | 392 | | return 1 |
| 0 | 393 | | endif |
| 0 | 394 | | isInf1 = v1 == Inf |
| 0 | 395 | | isInf2 = v2 == Inf |
| 0 | 396 | | if(isInf1 && isInf2) |
| 0 | 397 | | return 1 |
| 0 | 398 | | endif |
| 0 | 399 | | isnegInf1 = v1 == -Inf |
| 0 | 400 | | isnegInf2 = v2 == -Inf |
| 0 | 401 | | if(isnegInf1 && isnegInf2) |
| 0 | 402 | | return 1 |
| 0 | 403 | | endif |
| 0 | 404 | | if(v1 == v2) |
| 0 | 405 | | return 1 |
| 0 | 406 | | endif |
| | 407 | |
|
| 0 | 408 | | return 0 |
| 0 | 409 | | End |
| | 410 | |
|
| 0 | 411 | | static Function/S SPrintWaveElement(w, row, col, layer, chunk) |
| | 412 | | WAVE w |
| | 413 | | variable row, col, layer, chunk |
| | 414 | |
|
| 0 | 415 | | string str, tmpStr |
| 0 | 416 | | variable err |
| 0 | 417 | | variable majorType = WaveType(w, 1) |
| 0 | 418 | | variable minorType = WaveType(w) |
| | 419 | |
|
| 0 | 420 | | if(majorType == IUTF_WAVETYPE1_NUM) |
| 0 | 421 | | if(minorType & IUTF_WAVETYPE0_CMPL) |
| 0 | 422 | | if(minorType & (IUTF_WAVETYPE0_INT8 | IUTF_WAVETYPE0_INT16 | IUTF_WAVETYPE0_INT32)) |
| 0 | 423 | | Make/FREE/N=1/C/Y=(WaveType(w)) wTmp |
| 0 | 424 | | wTmp[0] = w[row][col][layer][chunk] |
| 0 | 425 | | if(minorType & IUTF_WAVETYPE0_USGN) |
| 0 | 426 | | wfprintf str, "(%u, %u)", wTmp |
| 0 | 427 | | else |
| 0 | 428 | | wfprintf str, "(%d, %d)", wTmp |
| 0 | 429 | | endif |
| 0 | 430 | | elseif(minorType & IUTF_WAVETYPE0_INT64) |
| | 431 | | #if IgorVersion() >= 7.00 |
| 0 | 432 | | if(minorType & IUTF_WAVETYPE0_USGN) |
| 0 | 433 | | WAVE/C/U/L wCLU = w |
| 0 | 434 | | Make/FREE/N=1/C/L/U wTmpCLU |
| 0 | 435 | | wTmpCLU[0] = wCLU[row][col][layer][chunk] |
| 0 | 436 | | wfprintf str, "(%u, %u)", wTmpCLU |
| 0 | 437 | | else |
| 0 | 438 | | WAVE/C/L wCLS = w |
| 0 | 439 | | Make/FREE/N=1/C/L wTmpCLS |
| 0 | 440 | | wTmpCLS[0] = wCLS[row][col][layer][chunk] |
| 0 | 441 | | wfprintf str, "(%d, %d)", wTmpCLS |
| 0 | 442 | | endif |
| | 443 | | #endif |
| 0 | 444 | | elseif(minorType & IUTF_WAVETYPE0_FP64) |
| 0 | 445 | | str = "(" + GetNiceStringForNumber(real(w[row][col][layer][chunk]), isDouble = 1) + ", " + GetNiceStringForNumbe |
| 0 | 446 | | else |
| 0 | 447 | | str = "(" + GetNiceStringForNumber(real(w[row][col][layer][chunk]), isDouble = 0) + ", " + GetNiceStringForNumbe |
| 0 | 448 | | endif |
| 0 | 449 | | else |
| 0 | 450 | | if(minorType & (IUTF_WAVETYPE0_INT8 | IUTF_WAVETYPE0_INT16 | IUTF_WAVETYPE0_INT32)) |
| 0 | 451 | | if(minorType & IUTF_WAVETYPE0_USGN) |
| 0 | 452 | | sprintf str, "%u", w[row][col][layer][chunk] |
| 0 | 453 | | else |
| 0 | 454 | | sprintf str, "%d", w[row][col][layer][chunk] |
| 0 | 455 | | endif |
| 0 | 456 | | elseif(minorType & IUTF_WAVETYPE0_INT64) |
| | 457 | | #if IgorVersion() >= 7.00 |
| 0 | 458 | | if(minorType & IUTF_WAVETYPE0_USGN) |
| 0 | 459 | | WAVE/U/L wLU = w |
| 0 | 460 | | sprintf str, "%u", wLU[row][col][layer][chunk] |
| 0 | 461 | | else |
| 0 | 462 | | WAVE/L wLS = w |
| 0 | 463 | | sprintf str, "%d", wLS[row][col][layer][chunk] |
| 0 | 464 | | endif |
| | 465 | | #endif |
| 0 | 466 | | elseif(minorType & IUTF_WAVETYPE0_FP64) |
| 0 | 467 | | str = GetNiceStringForNumber(w[row][col][layer][chunk], isDouble = 1) |
| 0 | 468 | | else |
| 0 | 469 | | str = GetNiceStringForNumber(w[row][col][layer][chunk], isDouble = 0) |
| 0 | 470 | | endif |
| 0 | 471 | | endif |
| 0 | 472 | | elseif(majorType == IUTF_WAVETYPE1_TEXT) |
| 0 | 473 | | // this should be done using DiffString |
| 0 | 474 | | WAVE/T wtext = w |
| 0 | 475 | | str = EscapeString(wtext[row][col][layer][chunk]) |
| 0 | 476 | | elseif(majorType == IUTF_WAVETYPE1_DFR) |
| 0 | 477 | | WAVE/DF wdfref = w |
| 0 | 478 | | tmpStr = GetDataFolder(1, wdfref[row][col][layer][chunk]) |
| 0 | 479 | | if(IsEmpty(tmpStr)) |
| 0 | 480 | | tmpStr = "_null DFR_" |
| 0 | 481 | | elseif(DataFolderRefStatus(wdfref[row][col][layer][chunk]) == 3) |
| 0 | 482 | | tmpStr = "_free DFR_" |
| 0 | 483 | | endif |
| | 484 | | #if IgorVersion() < 9.00 |
| 0 | 485 | | sprintf str, "0x%08x : %s", w[row][col][layer][chunk], tmpStr |
| | 486 | | #else |
| 0 | 487 | | if(GetRTError(0)) |
| 0 | 488 | | str = "_free_" |
| 0 | 489 | | else |
| 0 | 490 | | sprintf str, "0x%08x : %s", w[row][col][layer][chunk], tmpStr; err = GetRTError(1) |
| 0 | 491 | | endif |
| | 492 | | #endif |
| 0 | 493 | | elseif(majorType == IUTF_WAVETYPE1_WREF) |
| 0 | 494 | | WAVE/WAVE wref = w |
| 0 | 495 | | tmpStr = GetWavesDataFolder(wref[row][col][layer][chunk], 2) |
| 0 | 496 | | if(IsEmpty(tmpStr)) |
| 0 | 497 | | tmpStr = "_free wave_" |
| 0 | 498 | | endif |
| | 499 | | #if IgorVersion() < 9.00 |
| 0 | 500 | | sprintf str, "0x%08x : %s", w[row][col][layer][chunk], tmpStr |
| | 501 | | #else |
| 0 | 502 | | if(GetRTError(0)) |
| 0 | 503 | | str = "_free_" |
| 0 | 504 | | else |
| 0 | 505 | | sprintf str, "0x%08x : %s", w[row][col][layer][chunk], tmpStr; err = GetRTError(1) |
| 0 | 506 | | endif |
| | 507 | | #endif |
| 0 | 508 | | else |
| 0 | 509 | | sprintf str, "Unknown wave type" |
| 0 | 510 | | endif |
| | 511 | |
|
| 0 | 512 | | return str |
| 0 | 513 | | End |
| | 514 | |
|
| 0 | 515 | | static Function AddValueDiffImpl(table, wv1, wv2, locCount, row, col, layer, chunk, dimensions, runTol, tol) |
| | 516 | | WAVE/T table |
| | 517 | | WAVE wv1, wv2 |
| | 518 | | variable &locCount |
| | 519 | | variable row, col, layer, chunk, dimensions |
| | 520 | | variable &runTol |
| | 521 | | variable tol |
| | 522 | |
|
| 0 | 523 | | variable type1, type2, baseType1, baseType2 |
| 0 | 524 | | variable isInt1, isInt2, isInt641, isInt642, isComplex, v1, v2, isText1 |
| 0 | 525 | | variable bothWref, bothDFref |
| 0 | 526 | | variable curTol |
| 0 | 527 | | variable/C c1, c2 |
| 0 | 528 | | string s1, s2, str |
| 0 | 529 | | STRUCT IUTF_StringDiffResult strDiffResult |
| | 530 | | #if IgorVersion() >= 7.0 |
| 0 | 531 | | int64 cmpI64R |
| | 532 | | #endif |
| | 533 | |
|
| 0 | 534 | | baseType1 = WaveType(wv1, 1) |
| 0 | 535 | | baseType2 = WaveType(wv2, 1) |
| 0 | 536 | | type1 = WaveType(wv1) |
| 0 | 537 | | type2 = WaveType(wv2) |
| | 538 | |
|
| 0 | 539 | | bothWref = (baseType1 == IUTF_WAVETYPE1_WREF) && (baseType2 == IUTF_WAVETYPE1_WREF) |
| 0 | 540 | | bothDFref = (baseType1 == IUTF_WAVETYPE1_DFR) && (baseType2 == IUTF_WAVETYPE1_DFR) |
| 0 | 541 | | isText1 = baseType1 == IUTF_WAVETYPE1_TEXT |
| 0 | 542 | | if(istext1) |
| 0 | 543 | | WAVE/T wv1t = wv1 |
| 0 | 544 | | WAVE/T wv2t = wv2 |
| | 545 | |
|
| 0 | 546 | | s1 = wv1t[row][col][layer][chunk] |
| 0 | 547 | | s2 = wv2t[row][col][layer][chunk] |
| | 548 | |
|
| | 549 | | #if IgorVersion() >= 7.0 |
| 0 | 550 | | if(!CmpStr(s1, s2, 2)) |
| 0 | 551 | | return NaN |
| 0 | 552 | | endif |
| | 553 | | #else |
| 0 | 554 | | Make/FREE/T s1w = {s1} |
| 0 | 555 | | Make/FREE/T s2w = {s2} |
| | 556 | |
|
| 0 | 557 | | if(EqualWaves(s1w, s2w, WAVE_DATA)) |
| 0 | 558 | | return NaN |
| 0 | 559 | | endif |
| | 560 | | #endif |
| 0 | 561 | | elseif(bothWref) |
| 0 | 562 | | WAVE/WAVE wWRef1 = wv1 |
| 0 | 563 | | WAVE/WAVE wWRef2 = wv2 |
| 0 | 564 | | if(WaveRefsEqual(wWRef1[row][col][layer][chunk], wWRef2[row][col][layer][chunk])) |
| 0 | 565 | | return NaN |
| 0 | 566 | | endif |
| 0 | 567 | | elseif(bothDFref) |
| 0 | 568 | | WAVE/DF wDFRef1 = wv1 |
| 0 | 569 | | WAVE/DF wDFRef2 = wv2 |
| 0 | 570 | | if(DataFolderRefsEqual(wDFRef1[row][col][layer][chunk], wDFRef2[row][col][layer][chunk])) |
| 0 | 571 | | return NaN |
| 0 | 572 | | endif |
| 0 | 573 | | else |
| 0 | 574 | | isInt1 = type1 & (IUTF_WAVETYPE0_INT8 | IUTF_WAVETYPE0_INT16 | IUTF_WAVETYPE0_INT32 | IUTF_WAVETYPE0_INT64) |
| 0 | 575 | | isInt2 = type2 & (IUTF_WAVETYPE0_INT8 | IUTF_WAVETYPE0_INT16 | IUTF_WAVETYPE0_INT32 | IUTF_WAVETYPE0_INT64) |
| 0 | 576 | | isInt641 = type1 & IUTF_WAVETYPE0_INT64 |
| 0 | 577 | | isInt642 = type2 & IUTF_WAVETYPE0_INT64 |
| 0 | 578 | | isComplex = type1 & IUTF_WAVETYPE0_CMPL |
| | 579 | |
|
| 0 | 580 | | if(isInt1 && isInt2) |
| 0 | 581 | | // Int compare |
| 0 | 582 | | if(isInt641 || isInt642) |
| | 583 | | #if IgorVersion() >= 7.0 |
| 0 | 584 | | if(isComplex) |
| 0 | 585 | | Make/FREE/C/L/N=1 wInt64Diff |
| 0 | 586 | | wInt64Diff[0] = wv2[row][col][layer][chunk] - wv1[row][col][layer][chunk] |
| 0 | 587 | | v1 = real(wInt64Diff[0]) |
| 0 | 588 | | v2 = imag(wInt64Diff[0]) |
| 0 | 589 | | if(!v1 && !v2) |
| 0 | 590 | | return NaN |
| 0 | 591 | | elseif(tol != 0) |
| 0 | 592 | | curTol = v1 * v1 + v2 * v2 |
| 0 | 593 | | endif |
| 0 | 594 | | else |
| 0 | 595 | | cmpI64R = wv2[row][col][layer][chunk] - wv1[row][col][layer][chunk] |
| 0 | 596 | | if(!cmpI64R) |
| 0 | 597 | | return NaN |
| 0 | 598 | | elseif(tol != 0) |
| 0 | 599 | | curTol = cmpI64R * cmpI64R |
| 0 | 600 | | endif |
| 0 | 601 | | endif |
| | 602 | | #endif |
| 0 | 603 | | else |
| 0 | 604 | | if(isComplex) |
| 0 | 605 | | c1 = wv2[row][col][layer][chunk] - wv1[row][col][layer][chunk] |
| 0 | 606 | | v1 = real(c1) |
| 0 | 607 | | v2 = imag(c1) |
| 0 | 608 | | if(!v1 && !v2) |
| 0 | 609 | | return NaN |
| 0 | 610 | | elseif(tol != 0) |
| 0 | 611 | | curTol = v1 * v1 + v2 * v2 |
| 0 | 612 | | endif |
| 0 | 613 | | else |
| 0 | 614 | | v1 = wv1[row][col][layer][chunk] |
| 0 | 615 | | v2 = wv2[row][col][layer][chunk] |
| 0 | 616 | | if(v1 == v2) |
| 0 | 617 | | return NaN |
| 0 | 618 | | elseif(tol != 0) |
| 0 | 619 | | curTol = (v1 - v2) * (v1 - v2) |
| 0 | 620 | | endif |
| 0 | 621 | | endif |
| 0 | 622 | | endif |
| 0 | 623 | | else |
| 0 | 624 | | // FP compare |
| 0 | 625 | | if(isComplex) |
| 0 | 626 | | c1 = wv1[row][col][layer][chunk] |
| 0 | 627 | | c2 = wv2[row][col][layer][chunk] |
| 0 | 628 | | if(ValueEqualInclNaN(real(c1), real(c2)) && ValueEqualInclNaN(imag(c1), imag(c2))) |
| 0 | 629 | | return NaN |
| 0 | 630 | | elseif(tol != 0) |
| 0 | 631 | | v1 = GetToleranceValues(real(c1), real(c2)) |
| 0 | 632 | | v1 = IsFinite(v1) ? real(c1) - real(c2) : Inf |
| 0 | 633 | | v2 = GetToleranceValues(imag(c1), imag(c2)) |
| 0 | 634 | | v2 = IsFinite(v1) ? imag(c1) - imag(c2) : Inf |
| 0 | 635 | | curTol = v1 * v1 + v2 * v2 |
| 0 | 636 | | endif |
| 0 | 637 | | else |
| 0 | 638 | | v1 = wv1[row][col][layer][chunk] |
| 0 | 639 | | v2 = wv2[row][col][layer][chunk] |
| 0 | 640 | | if(ValueEqualInclNaN(v1, v2)) |
| 0 | 641 | | return NaN |
| 0 | 642 | | elseif(tol != 0) |
| 0 | 643 | | curTol = GetToleranceValues(v1, v2) |
| 0 | 644 | | endif |
| 0 | 645 | | endif |
| 0 | 646 | | endif |
| 0 | 647 | | endif |
| | 648 | |
|
| 0 | 649 | | if(!(istext1 | bothWref | bothDFref)) |
| 0 | 650 | | runTol += curTol |
| 0 | 651 | | if(runTol < tol) |
| 0 | 652 | | return NaN |
| 0 | 653 | | endif |
| 0 | 654 | | endif |
| | 655 | |
|
| 0 | 656 | | switch(dimensions) |
| 0 | 657 | | case 1: |
| 0 | 658 | | sprintf str, "[%d]", row |
| 0 | 659 | | break |
| 0 | 660 | | case 2: |
| 0 | 661 | | sprintf str, "[%d][%d]", row, col |
| 0 | 662 | | break |
| 0 | 663 | | case 3: |
| 0 | 664 | | sprintf str, "[%d][%d][%d]", row, col, layer |
| 0 | 665 | | break |
| 0 | 666 | | case 4: |
| 0 | 667 | | sprintf str, "[%d][%d][%d][%d]", row, col, layer, chunk |
| 0 | 668 | | break |
| 0 | 669 | | default: |
| 0 | 670 | | IUTF_Reporting#ReportErrorAndAbort("Unsupported number of dimensions") |
| 0 | 671 | | break |
| 0 | 672 | | endswitch |
| 0 | 673 | | table[2 * locCount][%DIMS] = str |
| 0 | 674 | | Make/FREE/T wDL1 = {GetDimLabel(wv1, UTF_ROW, row), GetDimLabel(wv1, UTF_COLUMN, col), GetDimLabel(wv1, UTF_LAYER, lay |
| 0 | 675 | | str = wDL1[0] + wDL1[1] + wDL1[2] + wDL1[3] |
| 0 | 676 | | if(!IsEmpty(str)) |
| 0 | 677 | | sprintf str, "%s;%s;%s;%s;", wDL1[0], wDL1[1], wDL1[2], wDL1[3] |
| 0 | 678 | | table[2 * locCount][%DIMLABEL] = str |
| 0 | 679 | | endif |
| 0 | 680 | | Make/FREE/T wDL2 = {GetDimLabel(wv2, UTF_ROW, row), GetDimLabel(wv2, UTF_COLUMN, col), GetDimLabel(wv2, UTF_LAYER, lay |
| 0 | 681 | | str = wDL2[0] + wDL2[1] + wDL2[2] + wDL2[3] |
| 0 | 682 | | if(!IsEmpty(str)) |
| 0 | 683 | | sprintf str, "%s;%s;%s;%s;", wDL2[0], wDL2[1], wDL2[2], wDL2[3] |
| 0 | 684 | | table[2 * locCount + 1][%DIMLABEL] = str |
| 0 | 685 | | endif |
| | 686 | |
|
| 0 | 687 | | if(istext1) |
| 0 | 688 | | WAVE/T wtext1 = wv1 |
| 0 | 689 | | WAVE/T wtext2 = wv2 |
| 0 | 690 | | string text1 = wtext1[row][col][layer][chunk] |
| 0 | 691 | | string text2 = wtext2[row][col][layer][chunk] |
| 0 | 692 | | DiffString(text1, text2, strDiffResult) |
| 0 | 693 | | table[2 * locCount][%ELEMENT] = strDiffResult.v1 |
| 0 | 694 | | table[2 * locCount + 1][%ELEMENT] = strDiffResult.v2 |
| 0 | 695 | | else |
| 0 | 696 | | table[2 * locCount][%ELEMENT] = SPrintWaveElement(wv1, row, col, layer, chunk) |
| 0 | 697 | | table[2 * locCount + 1][%ELEMENT] = SPrintWaveElement(wv2, row, col, layer, chunk) |
| 0 | 698 | | endif |
| | 699 | |
|
| 0 | 700 | | locCount += 1 |
| 0 | 701 | | End |
| | 702 | |
|
| | 703 | | // return the string representation of the number with no trailing zeros after the decimal point |
| 660 | 704 | | static Function/S GetNiceStringForNumber(n, [isDouble]) |
| | 705 | | variable n, isDouble |
| | 706 | |
|
| 660 | 707 | | variable precision |
| 660 | 708 | | string str |
| | 709 | |
|
| 660 | 710 | | isDouble = ParamIsDefault(isDouble) ? 0 : !!isDouble; |
| | 711 | |
|
| 660 | 712 | | precision = isDouble ? UTF_DECIMAL_DIGITS_FP64 : UTF_DECIMAL_DIGITS_FP32 |
| | 713 | |
|
| 660 | 714 | | sprintf str, "%.*g", precision, n |
| | 715 | |
|
| 660 | 716 | | return str |
| 660 | 717 | | End |
| | 718 | |
|
| | 719 | | // This function assumes that v1 != v2 and not both are NaN |
| 0 | 720 | | static Function GetToleranceValues(v1, v2) |
| | 721 | | variable v1, v2 |
| | 722 | |
|
| 0 | 723 | | variable isNaN1, isNaN2, isInf1, isInf2, isnegInf1, isnegInf2 |
| | 724 | |
|
| 0 | 725 | | isNaN1 = IsNaN(v1) |
| 0 | 726 | | isNaN2 = IsNaN(v2) |
| 0 | 727 | | if(isNaN1 || isNaN2) |
| 0 | 728 | | return Inf |
| 0 | 729 | | endif |
| 0 | 730 | | isInf1 = v1 == Inf |
| 0 | 731 | | isInf2 = v2 == Inf |
| 0 | 732 | | if(isInf1 != isInf2) |
| 0 | 733 | | return Inf |
| 0 | 734 | | endif |
| 0 | 735 | | isnegInf1 = v1 == -Inf |
| 0 | 736 | | isnegInf2 = v2 == -Inf |
| 0 | 737 | | if(isnegInf1 != isnegInf2) |
| 0 | 738 | | return Inf |
| 0 | 739 | | endif |
| | 740 | |
|
| 0 | 741 | | return (v1 - v2) * (v1 - v2) |
| 0 | 742 | | End |
| | 743 | |
|
| 0 | 744 | | static Function/S NicifyTableText(table, titleList) |
| | 745 | | WAVE/T table |
| | 746 | | string titleList |
| | 747 | |
|
| 0 | 748 | | variable numCols, numRows, i, j, padVal |
| 0 | 749 | | string str |
| | 750 | |
|
| 0 | 751 | | InsertPoints/M=(UTF_ROW) 0, 2, table |
| 0 | 752 | | numCols = min(DimSize(table, UTF_COLUMN), ItemsInList(titleList)) |
| 0 | 753 | | for(i = 0; i < numCols; i += 1) |
| 0 | 754 | | table[0][i] = StringFromList(i, titleList) |
| 0 | 755 | | endfor |
| | 756 | |
|
| 0 | 757 | | numCols = DimSize(table, UTF_COLUMN) |
| 0 | 758 | | numRows = DimSize(table, UTF_ROW) |
| 0 | 759 | | Make/FREE/N=(numRows, numCols)/D strSizes |
| | 760 | |
|
| 0 | 761 | | strSizes = strlen(table[p][q]) |
| | 762 | |
|
| 0 | 763 | | for(j = 0; j < numRows; j += 1) |
| 0 | 764 | | padVal = j == 1 ? 0x2d : 0x20 |
| 0 | 765 | | for(i = 0; i < numCols; i += 1) |
| | 766 | | #if IgorVersion() >= 7.00 |
| 0 | 767 | | WaveStats/Q/M=1/RMD=[][i, i] strSizes |
| | 768 | | #else |
| 0 | 769 | | Make/FREE/N=(DimSize(strSizes, UTF_ROW)) strSizeCol |
| 0 | 770 | | strSizeCol[] = strSizes[p][i] |
| 0 | 771 | | WaveStats/Q/M=1 strSizeCol |
| | 772 | | #endif |
| 0 | 773 | | table[j][i] = num2char(padVal) + PadString(table[j][i], V_Max, padVal) + num2char(padVal) |
| 0 | 774 | | endfor |
| 0 | 775 | | endfor |
| | 776 | |
|
| 0 | 777 | | str = "" |
| 0 | 778 | | for(j = 0; j < numRows; j += 1) |
| 0 | 779 | | for(i = 0; i < numCols; i += 1) |
| 0 | 780 | | str += table[j][i] + "|" |
| 0 | 781 | | endfor |
| 0 | 782 | | str += "\r" |
| 0 | 783 | | endfor |
| | 784 | |
|
| 0 | 785 | | return str |
| 0 | 786 | | End |
| | 787 | |
|
| | 788 | | /// @brief Based on DisplayHelpTopic "Character-by-Character Operations" |
| 0 | 789 | | static Function NumBytesInUTF8Character(str, byteOffset) |
| | 790 | | string str |
| | 791 | | variable byteOffset |
| | 792 | |
|
| 0 | 793 | | variable firstByte |
| 0 | 794 | | variable numBytesInString = strlen(str) |
| | 795 | |
|
| 0 | 796 | | if(byteOffset < 0 || byteOffset >= numBytesInString) |
| 0 | 797 | | return 0 |
| 0 | 798 | | endif |
| | 799 | |
|
| 0 | 800 | | firstByte = char2num(str[byteOffset]) & 0xFF |
| | 801 | |
|
| 0 | 802 | | if(firstByte < 0x80) |
| 0 | 803 | | return 1 |
| 0 | 804 | | endif |
| | 805 | |
|
| 0 | 806 | | if(firstByte >= 0xC2 && firstByte <= 0xDF) |
| 0 | 807 | | return 2 |
| 0 | 808 | | endif |
| | 809 | |
|
| 0 | 810 | | if(firstByte >= 0xE0 && firstByte <= 0xEF) |
| 0 | 811 | | return 3 |
| 0 | 812 | | endif |
| | 813 | |
|
| 0 | 814 | | if(firstByte >= 0xF0 && firstByte <= 0xF4) |
| 0 | 815 | | return 4 |
| 0 | 816 | | endif |
| | 817 | |
|
| 0 | 818 | | // Invalid UTF8 code point |
| 0 | 819 | | return 1 |
| 0 | 820 | | End |
| | 821 | |
|
| | 822 | | /// @brief Generate a diff of str1 and str2. This will only look for the first difference in both |
| | 823 | | /// strings. The strings are expected to be different! |
| | 824 | | /// |
| | 825 | | /// @param[in] str1 the first string |
| | 826 | | /// @param[in] str2 the second string |
| | 827 | | /// @param[out] result the diff of both strings |
| | 828 | | /// @param[in] case_sensitive (default: true) respecting the case during the diff |
| 0 | 829 | | static Function DiffString(str1, str2, result, [case_sensitive]) |
| | 830 | | string &str1 |
| | 831 | | string &str2 |
| | 832 | | STRUCT IUTF_StringDiffResult &result |
| | 833 | | variable case_sensitive |
| | 834 | |
|
| 0 | 835 | | variable start, line, end1, end2, endmin, diffpos |
| 0 | 836 | | variable str1len, str2len |
| 0 | 837 | | string lineEnding1, lineEnding2, prefix |
| | 838 | |
|
| 0 | 839 | | start = 0 |
| 0 | 840 | | line = 0 |
| 0 | 841 | | str1len = strlen(str1) |
| 0 | 842 | | str2len = strlen(str2) |
| 0 | 843 | | case_sensitive = ParamIsDefault(case_sensitive) ? 1 : case_sensitive |
| | 844 | |
|
| 0 | 845 | | // handle null strings |
| 0 | 846 | | if(IsNull(str1)) |
| 0 | 847 | | if(IsNull(str2)) |
| 0 | 848 | | IUTF_Reporting#ReportErrorAndAbort("Bug: Cannot create diff if both strings are null") |
| 0 | 849 | | endif |
| | 850 | |
|
| 0 | 851 | | result.v1 = "-:-:-> <NULL STRING>" |
| 0 | 852 | | end2 = DetectEndOfLine(str2, 0, lineEnding2) |
| 0 | 853 | | result.v2 = "0:0:0>" + GetStringWithContext(str2, 0, 0, end2 - 1) |
| 0 | 854 | | return NaN |
| 0 | 855 | | elseif(IsNull(str2)) |
| 0 | 856 | | end1 = DetectEndOfLine(str1, 0, lineEnding2) |
| 0 | 857 | | result.v1 = "0:0:0>" + GetStringWithContext(str1, 0, 0, end1 - 1) |
| 0 | 858 | | result.v2 = "-:-:-> <NULL STRING>" |
| 0 | 859 | | return NaN |
| 0 | 860 | | endif |
| | 861 | |
|
| 0 | 862 | | // The following cases can happen during the diff: |
| 0 | 863 | | // 1. text is different until line end |
| 0 | 864 | | // 2. one line is shorter than the other |
| 0 | 865 | | // 3. the line endings are different |
| 0 | 866 | | // 4. no differences in the current line |
| 0 | 867 | | // 5. one string is larger than the other one |
| 0 | 868 | | do |
| 0 | 869 | | end1 = DetectEndOfLine(str1, start, lineEnding1) |
| 0 | 870 | | end2 = DetectEndOfLine(str2, start, lineEnding2) |
| 0 | 871 | | endmin = min(end1, end2) |
| | 872 | |
|
| 0 | 873 | | diffpos = GetTextDiffPos(str1[start, endmin - 1], str2[start, endmin - 1], case_sensitive) |
| | 874 | |
|
| 0 | 875 | | // Case 1 |
| 0 | 876 | | if(diffpos >= 0) |
| 0 | 877 | | sprintf prefix, "%d:%d:%d>", line, diffpos, start + diffpos |
| 0 | 878 | | result.v1 = prefix + GetStringWithContext(str1, start, start + diffpos, end1 - 1) |
| 0 | 879 | | result.v2 = prefix + GetStringWithContext(str2, start, start + diffpos, end2 - 1) |
| 0 | 880 | | return NaN |
| 0 | 881 | | endif |
| | 882 | |
|
| 0 | 883 | | // Case 2 |
| 0 | 884 | | if(end1 != end2) |
| 0 | 885 | | sprintf prefix, "%d:%d:%d>", line, endmin - start, endmin |
| 0 | 886 | | result.v1 = prefix + GetStringWithContext(str1, start, endmin, end1) |
| 0 | 887 | | result.v2 = prefix + GetStringWithContext(str2, start, endmin, end2) |
| 0 | 888 | | return NaN |
| 0 | 889 | | endif |
| | 890 | |
|
| 0 | 891 | | // Case 3 |
| 0 | 892 | | if(CmpStr(lineEnding1, lineEnding2)) |
| 0 | 893 | | sprintf prefix, "%d:%d:%d>", line, endmin - start, endmin |
| 0 | 894 | | result.v1 = prefix + GetStringWithContext(str1, start, endmin, endmin + strlen(lineEnding1) - 1) |
| 0 | 895 | | result.v2 = prefix + GetStringWithContext(str2, start, endmin, endmin + strlen(lineEnding2) - 1) |
| 0 | 896 | | return NaN |
| 0 | 897 | | endif |
| | 898 | |
|
| 0 | 899 | | // Case 4 |
| 0 | 900 | | start = endmin + strlen(lineEnding1) |
| 0 | 901 | | line += 1 |
| | 902 | |
|
| 0 | 903 | | while(start < str1len && start < str2len) |
| | 904 | |
|
| 0 | 905 | | // Case 5 |
| 0 | 906 | | if(str1len != str2len) |
| 0 | 907 | | sprintf prefix, "%d:0:%d>", line, start |
| 0 | 908 | | if(str1len <= start) |
| 0 | 909 | | result.v1 = prefix |
| 0 | 910 | | else |
| 0 | 911 | | end1 = DetectEndOfLine(str1, start, lineEnding1) |
| 0 | 912 | | result.v1 = prefix + EscapeString(str1[start, end1]) |
| 0 | 913 | | endif |
| 0 | 914 | | if(str2len <= start) |
| 0 | 915 | | result.v2 = prefix |
| 0 | 916 | | else |
| 0 | 917 | | end2 = DetectEndOfLine(str2, start, lineEnding2) |
| 0 | 918 | | result.v2 = prefix + EscapeString(str2[start, end2]) |
| 0 | 919 | | endif |
| 0 | 920 | | return NaN |
| 0 | 921 | | endif |
| | 922 | |
|
| 0 | 923 | | IUTF_Reporting#ReportErrorAndAbort("Bug: Cannot create diff of equal strings") |
| 0 | 924 | | End |
| | 925 | |
|
| | 926 | | /// @brief Return a section of str which contains the character at diffpos and some context around. |
| | 927 | | /// The context will always be in the bounds of start and endpos. |
| | 928 | | /// |
| | 929 | | /// @param[in] str the string for which a section has to generated |
| | 930 | | /// @param[in] start the left-most bound to generate the context from |
| | 931 | | /// @param[in] diffpos the position of interest. A context will be generated around this position respecting start and e |
| | 932 | | /// @param[in] endpos the right-most bound to generate the context from |
| | 933 | | /// @returns the context at diffpos in str |
| 0 | 934 | | static Function/S GetStringWithContext(str, start, diffpos, endpos) |
| | 935 | | variable start, diffpos, endpos |
| | 936 | | string str |
| | 937 | |
|
| 0 | 938 | | string strOut |
| | 939 | |
|
| 0 | 940 | | strOut = EscapeString(str[max(start, diffpos - MAX_STRING_DIFF_CONTEXT), diffpos - 1]) |
| 0 | 941 | | strOut += EscapeString(str[diffpos, min(endpos, diffpos + MAX_STRING_DIFF_CONTEXT)]) |
| | 942 | |
|
| 0 | 943 | | return strOut |
| 0 | 944 | | End |
| | 945 | |
|
| | 946 | | /// @brief Get the first position with a difference in str1 and str2. |
| | 947 | | /// Since Igor 7.05 the case-sensitive check will be performed in byte mode. |
| | 948 | | /// |
| | 949 | | /// @param[in] str1 the first string |
| | 950 | | /// @param[in] str2 the second string |
| | 951 | | /// @param[in] case_sensitive the mode for case check. If this is set to 0 this will enforce the |
| | 952 | | /// case-insensitive check. All other values will use the default case-sensitive check |
| | 953 | | /// and since Igor 7.05 in byte mode. |
| | 954 | | /// @returns the position of the first difference. -1 if there is no difference. |
| 0 | 955 | | static Function GetTextDiffPos(str1, str2, case_sensitive) |
| | 956 | | string str1, str2 |
| | 957 | | variable case_sensitive |
| | 958 | |
|
| 0 | 959 | | variable i |
| 0 | 960 | | variable length = strlen(str1) |
| 0 | 961 | | variable mode = case_sensitive ? UTF_CMPSTR_MODE : 0 |
| | 962 | |
|
| 0 | 963 | | for(i = 0; i < length; i += 1) |
| 0 | 964 | | if(CmpStr(str1[i], str2[i], mode)) |
| 0 | 965 | | return i |
| 0 | 966 | | endif |
| 0 | 967 | | endfor |
| | 968 | |
|
| 0 | 969 | | return -1 |
| 0 | 970 | | End |
| | 971 | |
|
| | 972 | | /// @brief Detect the next end-of-line marker from the current start position. |
| | 973 | | /// This function supports the common line endings for Windows, Unix and OSX. |
| | 974 | | /// If there are no more line endings in str it will output the length of the string and an empty marker. |
| | 975 | | /// |
| | 976 | | /// @param[in] str a multi-line string for which a line ending has to be searched. |
| | 977 | | /// @param[in] start the start position inside str |
| | 978 | | /// @param[out] lineEnding the detected line ending marker. This can be "\r\n", "\r", "\n" or "". |
| | 979 | | /// returns the position where the line ending starts |
| 0 | 980 | | static Function DetectEndOfLine(str, start, lineEnding) |
| | 981 | | variable start |
| | 982 | | string str |
| | 983 | | string &lineEnding |
| | 984 | |
|
| 0 | 985 | | variable i |
| 0 | 986 | | variable length = strlen(str) |
| | 987 | |
|
| 0 | 988 | | lineEnding = "" |
| | 989 | |
|
| 0 | 990 | | for(i = start; i < length; i += 1) |
| 0 | 991 | | if(!CmpStr(str[i], "\r")) |
| 0 | 992 | | if(i + 1 < length && !CmpStr(str[i + 1], "\n")) |
| 0 | 993 | | lineEnding = "\r\n" |
| 0 | 994 | | else |
| 0 | 995 | | lineEnding = "\r" |
| 0 | 996 | | endif |
| 0 | 997 | | return i |
| 0 | 998 | | endif |
| 0 | 999 | | if(!CmpStr(str[i], "\n")) |
| 0 | 1000 | | lineEnding = "\n" |
| 0 | 1001 | | return i |
| 0 | 1002 | | endif |
| 0 | 1003 | | endfor |
| | 1004 | |
|
| 0 | 1005 | | // no line ending |
| 0 | 1006 | | return length |
| 0 | 1007 | | End |
| | 1008 | |
|
| | 1009 | | /// @brief Escaping a string by replacing any characters that are not printable in ASCII with a |
| | 1010 | | /// printable version. This has no support for other text encodings and is purely single |
| | 1011 | | /// byte aware. |
| | 1012 | | /// |
| | 1013 | | /// @param[in] str the string to escape |
| | 1014 | | /// @returns the escaped string |
| 0 | 1015 | | static Function/S EscapeString(str) |
| | 1016 | | string str |
| | 1017 | |
|
| 0 | 1018 | | variable i, charnum, length |
| 0 | 1019 | | string result, hex, char |
| | 1020 | |
|
| 0 | 1021 | | result = "" |
| 0 | 1022 | | length = strlen(str) |
| | 1023 | |
|
| 0 | 1024 | | for(i = 0; i < length; i += 1) |
| 0 | 1025 | | result += " " |
| 0 | 1026 | | char = str[i] |
| 0 | 1027 | | charnum = char2num(char) |
| 0 | 1028 | | if(charnum < 0) |
| 0 | 1029 | | charnum += 256 |
| 0 | 1030 | | endif |
| 0 | 1031 | | if(charnum >= ASCII_PRINTABLE_START && charnum <= ASCII_PRINTABLE_END) |
| 0 | 1032 | | result += str[i] |
| 0 | 1033 | | continue |
| 0 | 1034 | | endif |
| 0 | 1035 | | // non printable characters in ASCII |
| 0 | 1036 | | strswitch(char) |
| 0 | 1037 | | case "\000": |
| 0 | 1038 | | result += "<NUL>" |
| 0 | 1039 | | break |
| 0 | 1040 | | case "\n": |
| 0 | 1041 | | result += "<LF>" |
| 0 | 1042 | | break |
| 0 | 1043 | | case "\r": |
| 0 | 1044 | | result += "<CR>" |
| 0 | 1045 | | break |
| 0 | 1046 | | case "\t": |
| 0 | 1047 | | result += "<TAB>" |
| 0 | 1048 | | break |
| 0 | 1049 | | default: |
| 0 | 1050 | | sprintf hex, "<0x%02X>", charnum |
| 0 | 1051 | | result += hex |
| 0 | 1052 | | break |
| 0 | 1053 | | endswitch |
| 0 | 1054 | | endfor |
| | 1055 | |
|
| 0 | 1056 | | return result |
| 0 | 1057 | | End |