Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.2KB

  1. #include <stdio.h> /* printf */
  2. #include "utstring.h"
  3. int main()
  4. {
  5. UT_string *s,*t;
  6. char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s.";
  7. char V_NeedleStr[] = "needle\0s";
  8. long *V_KMP_Table;
  9. long V_FindPos;
  10. size_t V_StartPos;
  11. size_t V_FindCnt = 0;
  12. utstring_new(s);
  13. utstring_new(t);
  14. utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1);
  15. printf("\"%s\" len=%u\n", utstring_body(s), (unsigned)utstring_len(s));
  16. utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1);
  17. printf("\"%s\" len=%u\n", utstring_body(t), (unsigned)utstring_len(t));
  18. V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1));
  19. if (V_KMP_Table != NULL) {
  20. _utstring_BuildTableR(utstring_body(t), utstring_len(t), V_KMP_Table);
  21. V_StartPos = utstring_len(s) - 1;
  22. do {
  23. V_FindPos = _utstring_findR(utstring_body(s),
  24. V_StartPos + 1,
  25. utstring_body(t),
  26. utstring_len(t),
  27. V_KMP_Table);
  28. if (V_FindPos >= 0) {
  29. V_FindCnt++;
  30. V_StartPos = V_FindPos - 1;
  31. }
  32. printf("utstring_find()=%ld\n", V_FindPos);
  33. } while (V_FindPos >= 0);
  34. printf("FindCnt=%u\n", (unsigned)V_FindCnt);
  35. free(V_KMP_Table);
  36. } else {
  37. printf("malloc() failed...\n");
  38. }
  39. utstring_free(t);
  40. utstring_clear(s);
  41. utstring_printf(s,"ABC ABCDAB ABCDABCDABDE");
  42. int o;
  43. o=utstring_find( s, -9, "ABC", 3 ) ;
  44. printf("expect 15 %d\n",o);
  45. o=utstring_find( s, 3, "ABC", 3 ) ;
  46. printf("expect 4 %d\n",o);
  47. o=utstring_find( s, 16, "ABC", 3 ) ;
  48. printf("expect -1 %d\n",o);
  49. o=utstring_findR( s, -9, "ABC", 3 ) ;
  50. printf("expect 11 %d\n",o);
  51. o=utstring_findR( s, 12, "ABC", 3 ) ;
  52. printf("expect 4 %d\n",o);
  53. o=utstring_findR( s, 13, "ABC", 3 ) ;
  54. printf("expect 11 %d\n",o);
  55. o=utstring_findR( s, 2, "ABC", 3 ) ;
  56. printf("expect 0 %d\n",o);
  57. utstring_free(s);
  58. return 0;
  59. }