Просмотр исходного кода

utstring.h: utstring_find should be able to find within a const UT_string

Fixes #267.
Arthur O'Dwyer 7 месяцев назад
Родитель
Сommit
eb30caf530
4 измененных файлов с 44 добавлено и 7 удалено
  1. 6 6
      src/utstring.h
  2. 2 1
      tests/Makefile
  3. 2 0
      tests/test98.ans
  4. 34 0
      tests/test98.c

+ 6 - 6
src/utstring.h

@@ -139,7 +139,7 @@ UTSTRING_UNUSED static void utstring_printf_va(UT_string *s, const char *fmt, va
 #else
 #else
       va_copy(cp, ap);
       va_copy(cp, ap);
 #endif
 #endif
-      n = vsnprintf (&s->d[s->i], s->n-s->i, fmt, cp);
+      n = vsnprintf(&s->d[s->i], s->n-s->i, fmt, cp);
       va_end(cp);
       va_end(cp);
 
 
       if ((n > -1) && ((size_t) n < (s->n-s->i))) {
       if ((n > -1) && ((size_t) n < (s->n-s->i))) {
@@ -155,7 +155,7 @@ UTSTRING_UNUSED static void utstring_printf_va(UT_string *s, const char *fmt, va
 #ifdef __GNUC__
 #ifdef __GNUC__
 /* support printf format checking (2=the format string, 3=start of varargs) */
 /* support printf format checking (2=the format string, 3=start of varargs) */
 static void utstring_printf(UT_string *s, const char *fmt, ...)
 static void utstring_printf(UT_string *s, const char *fmt, ...)
-  __attribute__ (( format( printf, 2, 3) ));
+  __attribute__((format(printf, 2, 3)));
 #endif
 #endif
 UTSTRING_UNUSED static void utstring_printf(UT_string *s, const char *fmt, ...) {
 UTSTRING_UNUSED static void utstring_printf(UT_string *s, const char *fmt, ...) {
    va_list ap;
    va_list ap;
@@ -253,7 +253,7 @@ UTSTRING_UNUSED static long _utstring_find(
     size_t P_HaystackLen,
     size_t P_HaystackLen,
     const char *P_Needle,
     const char *P_Needle,
     size_t P_NeedleLen,
     size_t P_NeedleLen,
-    long *P_KMP_Table)
+    const long *P_KMP_Table)
 {
 {
     long i, j;
     long i, j;
     long V_FindPosition = -1;
     long V_FindPosition = -1;
@@ -286,7 +286,7 @@ UTSTRING_UNUSED static long _utstring_findR(
     size_t P_HaystackLen,
     size_t P_HaystackLen,
     const char *P_Needle,
     const char *P_Needle,
     size_t P_NeedleLen,
     size_t P_NeedleLen,
-    long *P_KMP_Table)
+    const long *P_KMP_Table)
 {
 {
     long i, j;
     long i, j;
     long V_FindPosition = -1;
     long V_FindPosition = -1;
@@ -316,7 +316,7 @@ UTSTRING_UNUSED static long _utstring_findR(
 
 
 /* Search data from left to right. ( One time search mode. ) */
 /* Search data from left to right. ( One time search mode. ) */
 UTSTRING_UNUSED static long utstring_find(
 UTSTRING_UNUSED static long utstring_find(
-    UT_string *s,
+    const UT_string *s,
     long P_StartPosition,   /* Start from 0. -1 means last position. */
     long P_StartPosition,   /* Start from 0. -1 means last position. */
     const char *P_Needle,
     const char *P_Needle,
     size_t P_NeedleLen)
     size_t P_NeedleLen)
@@ -362,7 +362,7 @@ UTSTRING_UNUSED static long utstring_find(
 
 
 /* Search data from right to left. ( One time search mode. ) */
 /* Search data from right to left. ( One time search mode. ) */
 UTSTRING_UNUSED static long utstring_findR(
 UTSTRING_UNUSED static long utstring_findR(
-    UT_string *s,
+    const UT_string *s,
     long P_StartPosition,   /* Start from 0. -1 means last position. */
     long P_StartPosition,   /* Start from 0. -1 means last position. */
     const char *P_Needle,
     const char *P_Needle,
     size_t P_NeedleLen)
     size_t P_NeedleLen)

+ 2 - 1
tests/Makefile

@@ -12,7 +12,8 @@ PROGS = test1 test2 test3 test4 test5 test6 test7 test8 test9   \
         test66 test67 test68 test69 test70 test71 test72 test73 \
         test66 test67 test68 test69 test70 test71 test72 test73 \
         test74 test75 test76 test77 test78 test79 test80 test81 \
         test74 test75 test76 test77 test78 test79 test80 test81 \
         test82 test83 test84 test85 test86 test87 test88 test89 \
         test82 test83 test84 test85 test86 test87 test88 test89 \
-        test90 test91 test92 test93 test94 test95 test96 test97
+        test90 test91 test92 test93 test94 test95 test96 test97 \
+        test98
 CFLAGS += -I$(HASHDIR)
 CFLAGS += -I$(HASHDIR)
 #CFLAGS += -DHASH_BLOOM=16
 #CFLAGS += -DHASH_BLOOM=16
 #CFLAGS += -O2
 #CFLAGS += -O2

+ 2 - 0
tests/test98.ans

@@ -0,0 +1,2 @@
+F: 2 2 2 3 9 9 9 9 9 9 -1 -1 2 2 2 3 9 9 9 9 9 9 -1 -1 -1
+R: -1 -1 2 3 3 3 3 3 3 9 9 9 -1 -1 2 3 3 3 3 3 3 9 9 9 9

+ 34 - 0
tests/test98.c

@@ -0,0 +1,34 @@
+#include <assert.h>
+#include <stdio.h>
+#include "utstring.h"
+
+int findl(const UT_string *haystack, int pos, char needle) {
+  return utstring_find(haystack, pos, &needle, 1);
+}
+int findr(const UT_string *haystack, int pos, char needle) {
+  return utstring_findR(haystack, pos, &needle, 1);
+}
+
+int main()
+{
+    UT_string *s;
+
+    utstring_new(s);
+    utstring_printf(s, "%s %s!", "hello", "world");
+    assert(utstring_len(s) == 12);
+
+    printf("F:");
+    for (int i = -12; i <= 12; ++i) {
+      printf(" %d", findl(s, i, 'l'));
+    }
+    printf("\n");
+
+    printf("R:");
+    for (int i = -12; i <= 12; ++i) {
+      printf(" %d", findr(s, i, 'l'));
+    }
+    printf("\n");
+
+    utstring_free(s);
+    return 0;
+}