You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/07/01 10:49:01 UTC

svn commit: r790073 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_string.h shared/string.c test/testcase.c

Author: mturk
Date: Wed Jul  1 08:49:01 2009
New Revision: 790073

URL: http://svn.apache.org/viewvc?rev=790073&view=rev
Log:
Add API for geting the number of tokens without modifying the string in advance

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h
    commons/sandbox/runtime/trunk/src/main/native/shared/string.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h?rev=790073&r1=790072&r2=790073&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_string.h Wed Jul  1 08:49:01 2009
@@ -187,6 +187,24 @@
 ACR_DECLARE(wchar_t *) ACR_wcstok_c(wchar_t *str, int sep, wchar_t **last);
 
 /**
+ * Determine the number of tokens in string without
+ * modifying it.
+ * @param str string to tokenize.
+ * @param sep Token delimiting character.
+ * @return Number of tokens.
+ */
+ACR_DECLARE(int) ACR_StrTokensA(const char *str, int sep);
+
+/**
+ * Determine the number of tokens in string without
+ * modifying it. Unicode version of the function.
+ * @param str string to tokenize.
+ * @param sep Token delimiting character.
+ * @return Number of tokens.
+ */
+ACR_DECLARE(int) ACR_StrTokensW(const wchar_t *str, int sep);
+
+/**
  * Count the number of string parts in multi string. Ansi version.
  * <p> Multi strings are zero separated double zero
  * terminated strings

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/string.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/string.c?rev=790073&r1=790072&r2=790073&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/string.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/string.c Wed Jul  1 08:49:01 2009
@@ -885,6 +885,44 @@
     }
 }
 
+ACR_DECLARE(int) ACR_StrTokensA(const char *str, int sep)
+{
+    int cnt = 1;
+
+    while (*str && *str == sep)          /* skip leading delimiters */
+        str++;
+    while (*str) {
+        if (*str == sep) {
+            while (*str == sep)
+                str++;
+            if (*str)
+                cnt++;
+        }
+        else
+            str++;
+    }
+    return cnt;
+}
+
+ACR_DECLARE(int) ACR_StrTokensW(const wchar_t *str, int sep)
+{
+    int cnt = 1;
+
+    while (*str && *str == (wchar_t)sep) /* skip leading delimiters */
+        str++;
+    while (*str) {
+        if (*str == sep) {
+            while (*str == sep)
+                str++;
+            if (*str)
+                cnt++;
+        }
+        else
+            str++;
+    }
+    return cnt;
+}
+
 ACR_DECLARE(wchar_t *) ACR_wcstok_c(wchar_t *str, int sep, wchar_t **last)
 {
     wchar_t *tok;

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=790073&r1=790072&r2=790073&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Jul  1 08:49:01 2009
@@ -350,12 +350,14 @@
 
 ACR_JNI_EXPORT_DECLARE(int, TestPrivate, test033)(ACR_JNISTDARGS, jint d)
 {
+    int e;
     int n = 0;
     char buf[64];
     char *token;
     char *state;
 
     sprintf(buf, "    1 22   3333  4");
+    e = ACR_StrTokensA(buf, ' ');
     token = ACR_strtok_c(buf, ' ', &state);
     if (token) {
         n++;
@@ -365,7 +367,7 @@
                 break;
         }
     }
-    return n;
+    return n == e ? n : 0;
 }