]> pd.if.org Git - pdclib/blobdiff - functions/string/strspn.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / string / strspn.c
index 6b0e586b79f661152d774d7ccbb1c28888649628..8869a846834607887a9c25bc50aef3f9baf2fc9c 100644 (file)
@@ -1,29 +1,47 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
+/* strspn( const char *, const char * )
 
-size_t strspn( const char * s1, const char * s2 ) { /* TODO */ };
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+#ifndef REGTEST
 
-/* PDPC code - unreviewed
+size_t strspn( const char * s1, const char * s2 )
 {
-    const char *p1;
-    const char *p2;
-    
-    p1 = s1;
-    while (*p1 != '\0')
+    size_t len = 0;
+    const char * p;
+    while ( s1[ len ] )
     {
-        p2 = s2;
-        while (*p2 != '\0')
+        p = s2;
+        while ( *p )
         {
-            if (*p1 == *p2) break;
-            p2++;
+            if ( s1[len] == *p )
+            {
+                break;
+            }
+            ++p;
         }
-        if (*p2 == '\0') return ((size_t)(p1 - s1));
-        p1++;
+        if ( ! *p )
+        {
+            return len;
+        }
+        ++len;
     }
-    return ((size_t)(p1 - s1));
+    return len;
 }
-*/
+
+#endif
+
+#ifdef TEST
+#include "_PDCLIB_test.h"
+
+int main( void )
+{
+    TESTCASE( strspn( abcde, "abc" ) == 3 );
+    TESTCASE( strspn( abcde, "b" ) == 0 );
+    TESTCASE( strspn( abcde, abcde ) == 5 );
+    return TEST_RESULTS;
+}
+#endif