Changeset 13 for trunk/src/helpers/stringh.c
- Timestamp:
- Nov 23, 2000, 7:36:41 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/stringh.c
r12 r13 3 3 *@@sourcefile stringh.c: 4 4 * contains string/text helper functions. These are good for 5 * parsing/splitting strings and other stuff used throughout XWorkplace. 5 * parsing/splitting strings and other stuff used throughout 6 * XWorkplace. 7 * 8 * Note that these functions are really a bunch of very mixed 9 * up string helpers, which you may or may not find helpful. 10 * If you're looking for string functions with memory 11 * management, look at xstring.c instead. 6 12 * 7 13 * Usage: All OS/2 programs. … … 180 186 * this creates a new PSZ containing the string 181 187 * from pBegin to pEnd, excluding the pEnd character. 182 * The new string is null-terminated. 188 * The new string is null-terminated. The caller 189 * must free() the new string after use. 183 190 * 184 191 * Example: … … 424 431 * be free()'able. 425 432 * 426 * Use of this wrapper is not recommended because427 * it is considerably slower than xstrrpl.433 * Repetitive use of this wrapper is not recommended 434 * because it is considerably slower than xstrrpl. 428 435 * 429 436 *@@added V0.9.6 (2000-11-01) [umoeller] … … 431 438 432 439 ULONG strhrpl(PSZ *ppszBuf, // in/out: string 433 ULONG ulOfs, // in: where to begin search (0 = start)434 const char *pcszSearch, // in: search string; cannot be NULL435 const char *pcsz Replace, // in: replacementstring; cannot be NULL436 PULONG pulAfterOfs) // out: offset where found (ptr can be NULL)440 PULONG pulOfs, // in: where to begin search (0 = start); 441 // out: ofs of first char after replacement string 442 const char *pcszSearch, // in: search string; cannot be NULL 443 const char *pcszReplace) // in: replacement string; cannot be NULL 437 444 { 438 445 ULONG ulrc = 0; … … 440 447 xstrFind, 441 448 xstrReplace; 449 size_t ShiftTable[256]; 450 BOOL fRepeat = FALSE; 442 451 xstrInit(&xstrBuf, 0); 443 452 xstrset(&xstrBuf, *ppszBuf); … … 447 456 xstrset(&xstrReplace, (PSZ)pcszReplace); 448 457 449 if (ulrc = xstrrpl(&xstrBuf, ulOfs, &xstrFind, &xstrReplace, pulAfterOfs)) 458 if ((ulrc = xstrrpl(&xstrBuf, 459 pulOfs, 460 &xstrFind, 461 &xstrReplace, 462 ShiftTable, 463 &fRepeat))) 450 464 // replaced: 451 465 *ppszBuf = xstrBuf.psz; … … 482 496 * converts a ULONG into a decimal string, while 483 497 * inserting thousands separators into it. Specify 484 * the separator char in cThousands. 498 * the separator character in cThousands. 499 * 485 500 * Returns pszTarget so you can use it directly 486 501 * with sprintf and the "%s" flag. 502 * 487 503 * For cThousands, you should use the data in 488 504 * OS2.INI ("PM_National" application), which is 489 505 * always set according to the "Country" object. 506 * You can use prfhQueryCountrySettings to 507 * retrieve this setting. 508 * 490 509 * Use strhThousandsDouble for "double" values. 491 510 */ … … 497 516 USHORT ust, uss, usc; 498 517 CHAR szTemp[40]; 499 sprintf(szTemp, "% d", ul);518 sprintf(szTemp, "%lu", ul); 500 519 501 520 ust = 0; … … 548 567 549 568 /* 569 *@@ strhVariableDouble: 570 * like strhThousandsULong, but for a "double" value, and 571 * with a variable number of decimal places depending on the 572 * size of the quantity. 573 * 574 *@@added V0.9.6 (2000-11-12) [pr] 575 */ 576 577 PSZ strhVariableDouble(PSZ pszTarget, 578 double dbl, 579 PSZ pszUnits, 580 CHAR cThousands) 581 { 582 if (dbl < 100.0) 583 sprintf(pszTarget, "%.2f%s", dbl, pszUnits); 584 else 585 if (dbl < 1000.0) 586 sprintf(pszTarget, "%.1f%s", dbl, pszUnits); 587 else 588 strcat(strhThousandsDouble(pszTarget, dbl, cThousands), 589 pszUnits); 590 591 return(pszTarget); 592 } 593 594 /* 550 595 *@@ strhFileDate: 551 596 * converts file date data to a string (to pszBuf). … … 566 611 * cDateSep is used as a date separator (e.g. '.'). 567 612 * This can be queried using: 568 + 613 + prfhQueryProfileChar(HINI_USER, "PM_National", "sDate", '/'); 569 614 * 570 615 * Alternatively, you can query all the country settings 571 * at once using prfhQueryCountrySettings (prfh.c , new with V0.9.0).616 * at once using prfhQueryCountrySettings (prfh.c). 572 617 * 573 618 *@@changed (99-11-07) [umoeller]: now calling strhDateTime … … 611 656 * 612 657 * Alternatively, you can query all the country settings 613 * at once using prfhQueryCountrySettings (prfh.c , new with V0.9.0).658 * at once using prfhQueryCountrySettings (prfh.c). 614 659 * 615 660 *@@changed 99-03-15 fixed 12-hour crash … … 637 682 /* 638 683 *@@ strhDateTime: 639 * converts Control Program eDATETIME info640 * to two strings. See strhFileDate and strhFileTime684 * converts Control Program DATETIME info 685 * into two strings. See strhFileDate and strhFileTime 641 686 * for more detailed parameter descriptions. 642 687 * … … 821 866 822 867 /* 868 *@@ strhIsWord: 869 * returns TRUE if p points to a "word" 870 * in pcszBuf. 871 * 872 * p is considered a word if the character _before_ 873 * it is in pcszBeginChars and the char _after_ 874 * it (i.e. *(p+cbSearch)) is in pcszEndChars. 875 * 876 *@@added V0.9.6 (2000-11-12) [umoeller] 877 */ 878 879 BOOL strhIsWord(const char *pcszBuf, 880 const char *p, // in: start of word 881 ULONG cbSearch, // in: length of word 882 const char *pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,." 883 const char *pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;" 884 { 885 BOOL fEndOK = FALSE; 886 887 // check previous char 888 if ( (p == pcszBuf) 889 || (strchr(pcszBeginChars, *(p-1))) 890 ) 891 { 892 // OK, valid begin char: 893 // check end char 894 CHAR cNextChar = *(p + cbSearch); 895 if (cNextChar == 0) 896 fEndOK = TRUE; 897 else 898 { 899 char *pc = strchr(pcszEndChars, cNextChar); 900 if (pc) 901 // OK, is end char: avoid doubles of that char, 902 // but allow spaces 903 if ( (cNextChar+1 != *pc) 904 || (cNextChar+1 == ' ') 905 || (cNextChar+1 == 0) 906 ) 907 fEndOK = TRUE; 908 } 909 } 910 911 return (fEndOK); 912 } 913 914 /* 823 915 *@@ strhFindWord: 824 916 * searches for pszSearch in pszBuf, which is … … 836 928 * 837 929 * The algorithm here uses strstr to find pszSearch in pszBuf 838 * and performs additional "is-word" checks for each item found. 839 * With VAC++ 3.0, this is still much faster than searching 840 * words first and then comparing each word with pszSearch. 841 * I've tried it that way too, and that took nearly double as 842 * long. Apparently, the VAC++ runtime library uses some 843 * optimized search algorithm here, so we better use that one. 930 * and performs additional "is-word" checks for each item found 931 * (by calling strhIsWord). 932 * 933 * Note that this function is fairly slow compared to xstrFindWord. 844 934 * 845 935 *@@added V0.9.0 (99-11-08) [umoeller] … … 859 949 { 860 950 const char *p = pszBuf; 861 862 /* // go thru all characters863 while (*p)864 {865 // check if current character is either the866 // very first or a "begin word" character867 if ( (p == pszBuf)868 || (strchr(pcszBeginChars, *p) == 0)869 )870 {871 // yes: go for next872 if (*(++p))873 {874 // compare with search string875 if (strcmp(p, pszSearch) == 0)876 {877 // is the same:878 // check if still in buffer879 if (p < pszBuf + cbBuf)880 {881 CHAR cAfterEndOfWord = *(p + cbSearch);882 if (cAfterEndOfWord == 0)883 {884 // end of string:885 // that's ok886 pszReturn = (PSZ)p;887 break;888 }889 else890 {891 // check if in "end of word" list892 char *pc2 = strchr(pcszEndChars, cAfterEndOfWord);893 if (pc2)894 // OK, is end char: avoid doubles of that char,895 // but allow spaces896 if ( (cAfterEndOfWord+1 != *pc2)897 || (cAfterEndOfWord+1 == ' ')898 || (cAfterEndOfWord+1 == 0)899 )900 {901 // end of string:902 // that's ok903 pszReturn = (PSZ)p;904 break;905 }906 }907 }908 }909 }910 else911 // end of string:912 break;913 }914 915 ++p;916 } // end while917 */918 951 919 952 do // while p … … 925 958 // check if that's a word 926 959 927 // check previous char 928 if ( (p == pszBuf) 929 || (strchr(pcszBeginChars, *(p-1))) 930 ) 960 if (strhIsWord(pszBuf, 961 p, 962 cbSearch, 963 pcszBeginChars, 964 pcszEndChars)) 931 965 { 932 // OK, valid begin char: 933 BOOL fEndOK = FALSE; 934 // check end char 935 CHAR cNextChar = *(p + cbSearch); 936 if (cNextChar == 0) 937 fEndOK = TRUE; 938 else 939 { 940 char *pc = strchr(pcszEndChars, cNextChar); 941 if (pc) 942 // OK, is end char: avoid doubles of that char, 943 // but allow spaces 944 if ( (cNextChar+1 != *pc) 945 || (cNextChar+1 == ' ') 946 || (cNextChar+1 == 0) 947 ) 948 fEndOK = TRUE; 949 } 950 951 if (fEndOK) 952 { 953 // valid end char: 954 pszReturn = (PSZ)p; 955 break; 956 } 966 // valid end char: 967 pszReturn = (PSZ)p; 968 break; 957 969 } 970 958 971 p += cbSearch; 959 972 } … … 1186 1199 if (pEOL) 1187 1200 { 1188 XSTRING strBuf, 1189 strFind, 1190 strReplace; 1201 XSTRING strBuf; 1202 ULONG ulOfs = 0; 1191 1203 1192 1204 PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1); … … 1196 1208 xstrInit(&strBuf, 0); 1197 1209 xstrset(&strBuf, *ppszBuf); // this must not be freed! 1198 xstrInit(&strFind, 0);1210 /* xstrInit(&strFind, 0); 1199 1211 xstrset(&strFind, pszOldCopy); // this must not be freed! 1200 1212 xstrInit(&strReplace, 0); 1201 1213 xstrset(&strReplace, pszNewParam); // this must not be freed! 1214 */ 1202 1215 1203 1216 // check for upper case desired? … … 1206 1219 strupr(pszNewParam); 1207 1220 1208 xstr rpl(&strBuf, 0, &strFind, &strReplace, NULL);1221 xstrcrpl(&strBuf, &ulOfs, pszOldCopy, pszNewParam); 1209 1222 1210 1223 free(pszOldCopy); … … 1373 1386 PSZ pParam; 1374 1387 if ((pParam = strhFindAttribValue(pszSearchIn, pszTag))) 1375 sscanf(pParam, "% d", pl);1388 sscanf(pParam, "%ld", pl); 1376 1389 1377 1390 return (pParam); … … 1799 1812 pszLine += ulIndent; 1800 1813 } 1801 pszLine += sprintf(pszLine, "%02lX ", *pbCurrent);1814 pszLine += sprintf(pszLine, "%02lX ", (ULONG)*pbCurrent); 1802 1815 1803 1816 if ( (*pbCurrent > 31) && (*pbCurrent < 127) ) … … 1869 1882 1870 1883 /* 1871 * @@skip_comp_os2:1884 * skip_comp_os2: 1872 1885 * Return a pointer to the next component of the path SRC, for OS/2 1873 1886 * and DOS styles. When the end of the string is reached, a pointer … … 2467 2480 2468 2481 /* 2469 *@@ strhfind: 2470 * searches for a pattern in a string using the Boyer-Moore- 2471 * Horspool-Sunday algorithm. The string and pattern are null-terminated 2472 * strings. Returns a pointer to the pattern if found within the string, 2473 * or NULL if the pattern was not found. If you repeatedly scan for the 2474 * same pattern, use the repeat_find argument. If this is TRUE, the 2475 * function does not re-parse the pattern. You must of course call the 2476 * function with repeat_find equal to FALSE the first time. This function 2477 * is meant to handle character data, and is most effective when you work 2478 * with large strings. To search binary data use strhmemfind(). Will not work 2479 * on multibyte characters. 2480 * 2481 * Examples: 2482 + char *result; 2482 *@@ strhmemfind: 2483 * searches for a pattern in a block of memory using the 2484 * Boyer-Moore-Horspool-Sunday algorithm. 2485 * 2486 * The block and pattern may contain any values; you must 2487 * explicitly provide their lengths. If you search for strings, 2488 * use strlen() on the buffers. 2489 * 2490 * Returns a pointer to the pattern if found within the block, 2491 * or NULL if the pattern was not found. 2492 * 2493 * This algorithm needs a "shift table" to cache data for the 2494 * search pattern. This table can be reused when performing 2495 * several searches with the same pattern. 2496 * 2497 * "shift" must point to an array big enough to hold 256 (8**2) 2498 * "size_t" values. 2499 * 2500 * If (*repeat_find == FALSE), the shift table is initialized. 2501 * So on the first search with a given pattern, *repeat_find 2502 * should be FALSE. This function sets it to TRUE after the 2503 * shift table is initialised, allowing the initialisation 2504 * phase to be skipped on subsequent searches. 2505 * 2506 * This function is most effective when repeated searches are 2507 * made for the same pattern in one or more large buffers. 2508 * 2509 * Example: 2510 * 2511 + PSZ pszHaystack = "This is a sample string.", 2512 + pszNeedle = "string"; 2513 + size_t shift[256]; 2514 + BOOL fRepeat = FALSE; 2483 2515 + 2484 + result = strhfind ("abracadabra", "cad", FALSE); 2485 + if (result) 2486 + puts (result); 2487 + 2516 + PSZ pFound = strhmemfind(pszHaystack, 2517 + strlen(pszHaystack), // block size 2518 + pszNeedle, 2519 + strlen(pszNeedle), // pattern size 2520 + shift, 2521 + &fRepeat); 2522 * 2488 2523 * Taken from the "Standard Function Library", file sflfind.c. 2489 2524 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2490 * Slightly modified .2525 * Slightly modified by umoeller. 2491 2526 * 2492 2527 *@@added V0.9.3 (2000-05-08) [umoeller] 2493 2528 */ 2494 2529 2495 char* strhfind (const char *string, // String containing data 2496 const char *pattern, // Pattern to search for 2497 BOOL repeat_find) // Same pattern as last time 2498 { 2499 static size_t 2500 searchbuf [256]; // Fixed search buffer 2501 2502 ASSERT (string); // Expect non-NULL pointers, but 2503 ASSERT (pattern); // fall through if not debugging 2504 2505 return (char *) strhmemfind_rb (string, strlen (string), 2506 pattern, strlen (pattern), 2507 searchbuf, &repeat_find); 2508 } 2509 2510 /* 2511 *@@ strhfind_r: 2512 * searches for a pattern in a string using the Boyer-Moore- 2513 * Horspool-Sunday algorithm. The string and pattern are null-terminated 2514 * strings. Returns a pointer to the pattern if found within the string, 2515 * or NULL if the pattern was not found. This function is meant to handle 2516 * character data, and is most effective when you work with large strings. 2517 * To search binary data use strhmemfind(). Will not work on multibyte 2518 * characters. Reentrant. 2519 * 2520 * Examples: 2521 + char *result; 2522 + 2523 + result = strhfind_r ("abracadabra", "cad"); 2524 + if (result) 2525 + puts (result); 2526 * 2527 * Taken from the "Standard Function Library", file sflfind.c. 2528 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2529 * Slightly modified. 2530 * 2531 *@@added V0.9.3 (2000-05-08) [umoeller] 2532 */ 2533 2534 char* strhfind_r (const char *string, // String containing data 2535 const char *pattern) // Pattern to search for 2536 { 2537 size_t 2538 searchbuf [256]; // One-time search buffer 2539 BOOL 2540 secondtime = FALSE; // Search buffer init needed 2541 2542 ASSERT (string); // Expect non-NULL pointers, but 2543 ASSERT (pattern); // fall through if not debugging 2544 2545 return (char *) strhmemfind_rb (string, strlen (string), 2546 pattern, strlen (pattern), 2547 searchbuf, &secondtime); 2548 } 2549 2550 /* 2551 *@@ strhfind_rb: 2552 * searches for a pattern in a string using the Boyer-Moore- 2553 * Horspool-Sunday algorithm. The string and pattern are null-terminated 2554 * strings. Returns a pointer to the pattern if found within the string, 2555 * or NULL if the pattern was not found. Supports more efficient repeat 2556 * searches (for the same pattern), through a supplied search buffer. The 2557 * search buffer must be long enough to contain 256 (2**8) size_t entries. 2558 * On the first call repeat_find must be set to FALSE. After the search 2559 * buffer has been initialised, repeat_find will be set to TRUE by the 2560 * function, avoiding the search buffer initialisation on later calls. 2561 * 2562 * This function is most effective when repeated searches are made for 2563 * the same pattern in one or more strings. This function is meant to 2564 * handle character data, and is most effective when you work with 2565 * large strings. To search binary data use strhmemfind(). Will not work 2566 * on multibyte characters. Reentrant. 2567 * 2568 * Examples: 2569 + char *result; 2570 + BOOL repeat_search = FALSE; 2571 + size_t searchbuf[256]; 2572 + 2573 + result = strhfind_rb ("abracadabra", "cad", searchbuf, &repeat_search); 2574 + if (result) 2575 + { 2576 + puts (result); 2577 + result = strhfind_rb ("cad/cam", "cad", searchbuf, &repeat_search); 2578 + if (result) 2579 + puts (result); 2580 + } 2581 * 2582 * Taken from the "Standard Function Library", file sflfind.c. 2583 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2584 * Slightly modified. 2585 * 2586 *@@added V0.9.3 (2000-05-08) [umoeller] 2587 */ 2588 2589 char* strhfind_rb (const char *string, // String containing data 2590 const char *pattern, // Pattern to search for 2591 size_t *shift, // Working buffer between searches 2592 BOOL *repeat_find) // Flag for first/later search 2593 { 2594 ASSERT (string); // Expect non-NULL pointers, but 2595 ASSERT (pattern); // fall through if not debugging 2596 ASSERT (shift); 2597 ASSERT (repeat_find); 2598 2599 return (char *) strhmemfind_rb (string, strlen (string), 2600 pattern, strlen (pattern), 2601 shift, repeat_find); 2602 } 2603 2604 /* 2605 *@@ strhmemfind: 2606 * searches for a pattern in a block of memory using the Boyer- 2607 * Moore-Horspool-Sunday algorithm. The block and pattern may contain any 2608 * values; you must explicitly provide their lengths. Returns a pointer to 2609 * the pattern if found within the block, or NULL if the pattern was not 2610 * found. If you repeatedly scan for the same pattern, use the repeat_find 2611 * argument. If this is TRUE, the function does not re-parse the pattern. 2612 * This function is meant to handle binary data. If you need to search 2613 * strings, use the strhfind_r or strhfind_rb() functions. Non-Reentrant. 2614 * 2615 * Taken from the "Standard Function Library", file sflfind.c. 2616 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2617 * Slightly modified. 2618 * 2619 *@@added V0.9.3 (2000-05-08) [umoeller] 2620 */ 2621 2622 void* strhmemfind (const void *block, // Block containing data 2623 size_t block_size, // Size of block in bytes 2624 const void *pattern, // Pattern to search for 2625 size_t pattern_size, // Size of pattern block 2626 BOOL repeat_find) // Same pattern as last time 2627 { 2628 static size_t 2629 searchbuf [256]; // Static shared search buffer 2630 2631 ASSERT (block); // Expect non-NULL pointers, but 2632 ASSERT (pattern); // full through if not debugging 2633 2634 return strhmemfind_rb (block, block_size, pattern, pattern_size, 2635 searchbuf, &repeat_find); 2636 } 2637 2638 /* 2639 *@@ strhmemfind_r: 2640 * searches for a pattern in a block of memory using the Boyer- 2641 * Moore-Horspool-Sunday algorithm. The block and pattern may contain any 2642 * values; you must explicitly provide their lengths. Returns a pointer to 2643 * the pattern if found within the block, or NULL if the pattern was not 2644 * found. 2645 * 2646 * This function is meant to handle binary data, for a single search for 2647 * a given pattern. If you need to search strings, use the strhfind_r() 2648 * or strhfind_rb() functions. If you want to do efficient repeated searches 2649 * for one pattern, use strhmemfind_rb(). Reentrant. 2650 * 2651 * Taken from the "Standard Function Library", file sflfind.c. 2652 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2653 * Slightly modified. 2654 * 2655 *@@added V0.9.3 (2000-05-08) [umoeller] 2656 */ 2657 2658 void* strhmemfind_r (const void *block, // Block containing data 2659 size_t block_size, // Size of block in bytes 2660 const void *pattern, // Pattern to search for 2661 size_t pattern_size) // Size of pattern block 2662 { 2663 size_t 2664 searchbuf [256]; // One-time search buffer 2665 BOOL 2666 secondtime = FALSE; 2667 2668 ASSERT (block); // Expect non-NULL pointers, but 2669 ASSERT (pattern); // full through if not debugging 2670 2671 return strhmemfind_rb (block, block_size, pattern, pattern_size, 2672 searchbuf, &secondtime); 2673 } 2674 2675 /* 2676 *@@ strhmemfind_rb: 2677 * searches for a pattern in a block of memory using the Boyer- 2678 * Moore-Horspool-Sunday algorithm. The block and pattern may contain any 2679 * values; you must explicitly provide their lengths. Returns a pointer to 2680 * the pattern if found within the block, or NULL if the pattern was not 2681 * found. On the first search with a given pattern, *repeat_find should 2682 * be FALSE. It will be set to TRUE after the shift table is initialised, 2683 * allowing the initialisation phase to be skipped on subsequent searches. 2684 * shift must point to an array big enough to hold 256 (8**2) size_t values. 2685 * 2686 * This function is meant to handle binary data, for repeated searches 2687 * for the same pattern. If you need to search strings, use the 2688 * strhfind_r() or strhfind_rb() functions. If you wish to search for a 2689 * pattern only once consider using strhmemfind_r(). Reentrant. 2690 * 2691 * Taken from the "Standard Function Library", file sflfind.c. 2692 * Copyright: Copyright (c) 1991-99 iMatix Corporation. 2693 * Slightly modified. 2694 * 2695 *@@added V0.9.3 (2000-05-08) [umoeller] 2696 */ 2697 2698 void* strhmemfind_rb (const void *in_block, // Block containing data 2699 size_t block_size, // Size of block in bytes 2700 const void *in_pattern, // Pattern to search for 2701 size_t pattern_size, // Size of pattern block 2702 size_t *shift, // Shift table (search buffer) 2703 BOOL *repeat_find) // TRUE: search buffer already init 2704 { 2705 size_t 2706 byte_nbr, // Distance through block 2707 match_size; // Size of matched part 2530 void* strhmemfind(const void *in_block, // in: block containing data 2531 size_t block_size, // in: size of block in bytes 2532 const void *in_pattern, // in: pattern to search for 2533 size_t pattern_size, // in: size of pattern block 2534 size_t *shift, // in/out: shift table (search buffer) 2535 BOOL *repeat_find) // in/out: if TRUE, *shift is already initialized 2536 { 2537 size_t byte_nbr, // Distance through block 2538 match_size; // Size of matched part 2708 2539 const unsigned char 2709 *match_base = NULL, // Base of match of pattern2710 *match_ptr = NULL, // Point within current match2711 *limit = NULL; // Last potiental match point2540 *match_base = NULL, // Base of match of pattern 2541 *match_ptr = NULL, // Point within current match 2542 *limit = NULL; // Last potiental match point 2712 2543 const unsigned char 2713 *block = (unsigned char *) in_block, // Concrete pointer to block data2714 *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value2715 2716 ASSERT (block); // Expect non-NULL pointers, but2717 ASSERT (pattern); // fail gracefully if not debugging2718 ASSERT (shift); // NULL repeat_find => is false2719 if (block == NULL || pattern == NULL || shift == NULL)2544 *block = (unsigned char *) in_block, // Concrete pointer to block data 2545 *pattern = (unsigned char *) in_pattern; // Concrete pointer to search value 2546 2547 if ( (block == NULL) 2548 || (pattern == NULL) 2549 || (shift == NULL) 2550 ) 2720 2551 return (NULL); 2721 2552 … … 2736 2567 2737 2568 if (!repeat_find || !*repeat_find) 2738 { 2739 for (byte_nbr = 0; byte_nbr < 256; byte_nbr++) 2740 shift [byte_nbr] = pattern_size + 1; 2741 for (byte_nbr = 0; byte_nbr < pattern_size; byte_nbr++) 2742 shift [(unsigned char) pattern [byte_nbr]] = pattern_size - byte_nbr; 2569 { 2570 for (byte_nbr = 0; 2571 byte_nbr < 256; 2572 byte_nbr++) 2573 shift[byte_nbr] = pattern_size + 1; 2574 for (byte_nbr = 0; 2575 byte_nbr < pattern_size; 2576 byte_nbr++) 2577 shift[(unsigned char)pattern[byte_nbr]] = pattern_size - byte_nbr; 2743 2578 2744 2579 if (repeat_find) 2745 2580 *repeat_find = TRUE; 2746 2581 } 2747 2582 2748 2583 // Search for the block, each time jumping up by the amount … … 2754 2589 for (match_base = block; 2755 2590 match_base < limit; 2756 match_base += shift 2757 2591 match_base += shift[*(match_base + pattern_size)]) 2592 { 2758 2593 match_ptr = match_base; 2759 2594 match_size = 0; 2760 2595 2761 2596 // Compare pattern until it all matches, or we find a difference 2762 while (*match_ptr++ == pattern 2763 2597 while (*match_ptr++ == pattern[match_size++]) 2598 { 2764 2599 ASSERT (match_size <= pattern_size && 2765 2600 match_ptr == (match_base + match_size)); 2766 2601 2767 // 2602 // If we found a match, return the start address 2768 2603 if (match_size >= pattern_size) 2769 return ((void*)(match_base));2770 2771 2772 2604 return ((void*)(match_base)); 2605 2606 } 2607 } 2773 2608 return (NULL); // Found nothing 2774 2609 }
Note:
See TracChangeset
for help on using the changeset viewer.