Back to index

php5  5.3.10
Functions
split.c File Reference
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Functions

int split (char *string, fields, int nfields, char *sep)

Function Documentation

int split ( char *  string,
fields  ,
int  nfields,
char *  sep 
)

Definition at line 9 of file split.c.

{
       register char *p = string;
       register char c;                   /* latest character */
       register char sepc = sep[0];
       register char sepc2;
       register int fn;
       register char **fp = fields;
       register char *sepp;
       register int trimtrail;

       /* white space */
       if (sepc == '\0') {
              while ((c = *p++) == ' ' || c == '\t')
                     continue;
              p--;
              trimtrail = 1;
              sep = " \t";  /* note, code below knows this is 2 long */
              sepc = ' ';
       } else
              trimtrail = 0;
       sepc2 = sep[1];             /* now we can safely pick this up */

       /* catch empties */
       if (*p == '\0')
              return(0);

       /* single separator */
       if (sepc2 == '\0') {
              fn = nfields;
              for (;;) {
                     *fp++ = p;
                     fn--;
                     if (fn == 0)
                            break;
                     while ((c = *p++) != sepc)
                            if (c == '\0')
                                   return(nfields - fn);
                     *(p-1) = '\0';
              }
              /* we have overflowed the fields vector -- just count them */
              fn = nfields;
              for (;;) {
                     while ((c = *p++) != sepc)
                            if (c == '\0')
                                   return(fn);
                     fn++;
              }
              /* not reached */
       }

       /* two separators */
       if (sep[2] == '\0') {
              fn = nfields;
              for (;;) {
                     *fp++ = p;
                     fn--;
                     while ((c = *p++) != sepc && c != sepc2)
                            if (c == '\0') {
                                   if (trimtrail && **(fp-1) == '\0')
                                          fn++;
                                   return(nfields - fn);
                            }
                     if (fn == 0)
                            break;
                     *(p-1) = '\0';
                     while ((c = *p++) == sepc || c == sepc2)
                            continue;
                     p--;
              }
              /* we have overflowed the fields vector -- just count them */
              fn = nfields;
              while (c != '\0') {
                     while ((c = *p++) == sepc || c == sepc2)
                            continue;
                     p--;
                     fn++;
                     while ((c = *p++) != '\0' && c != sepc && c != sepc2)
                            continue;
              }
              /* might have to trim trailing white space */
              if (trimtrail) {
                     p--;
                     while ((c = *--p) == sepc || c == sepc2)
                            continue;
                     p++;
                     if (*p != '\0') {
                            if (fn == nfields+1)
                                   *p = '\0';
                            fn--;
                     }
              }
              return(fn);
       }

       /* n separators */
       fn = 0;
       for (;;) {
              if (fn < nfields)
                     *fp++ = p;
              fn++;
              for (;;) {
                     c = *p++;
                     if (c == '\0')
                            return(fn);
                     sepp = sep;
                     while ((sepc = *sepp++) != '\0' && sepc != c)
                            continue;
                     if (sepc != '\0')    /* it was a separator */
                            break;
              }
              if (fn < nfields)
                     *(p-1) = '\0';
              for (;;) {
                     c = *p++;
                     sepp = sep;
                     while ((sepc = *sepp++) != '\0' && sepc != c)
                            continue;
                     if (sepc == '\0')    /* it wasn't a separator */
                            break;
              }
              p--;
       }

       /* not reached */
}