]> granicus.if.org Git - postgresql/blobdiff - contrib/test_parser/test_parser.c
Adjust blank lines around PG_MODULE_MAGIC defines, for consistency
[postgresql] / contrib / test_parser / test_parser.c
index 784d2d43ad02606f311cfd3f00efb277f90bf77f..cbf77966ae57a1528399a6e56b735d76fae6ccc5 100644 (file)
@@ -3,10 +3,10 @@
  * test_parser.c
  *       Simple example of a text search parser
  *
- * Copyright (c) 2007, PostgreSQL Global Development Group
+ * Copyright (c) 2007-2014, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/contrib/test_parser/test_parser.c,v 1.2 2007/11/15 21:14:31 momjian Exp $
+ *       contrib/test_parser/test_parser.c
  *
  *-------------------------------------------------------------------------
  */
@@ -16,7 +16,6 @@
 
 PG_MODULE_MAGIC;
 
-
 /*
  * types
  */
@@ -27,7 +26,7 @@ typedef struct
        char       *buffer;                     /* text to parse */
        int                     len;                    /* length of the text in buffer */
        int                     pos;                    /* position of the parser */
-}      ParserState;
+} ParserState;
 
 /* copy-paste from wparser.h of tsearch2 */
 typedef struct
@@ -35,26 +34,16 @@ typedef struct
        int                     lexid;
        char       *alias;
        char       *descr;
-}      LexDescr;
+} LexDescr;
 
 /*
- * prototypes
+ * functions
  */
 PG_FUNCTION_INFO_V1(testprs_start);
-Datum          testprs_start(PG_FUNCTION_ARGS);
-
 PG_FUNCTION_INFO_V1(testprs_getlexeme);
-Datum          testprs_getlexeme(PG_FUNCTION_ARGS);
-
 PG_FUNCTION_INFO_V1(testprs_end);
-Datum          testprs_end(PG_FUNCTION_ARGS);
-
 PG_FUNCTION_INFO_V1(testprs_lextype);
-Datum          testprs_lextype(PG_FUNCTION_ARGS);
 
-/*
- * functions
- */
 Datum
 testprs_start(PG_FUNCTION_ARGS)
 {
@@ -73,31 +62,32 @@ testprs_getlexeme(PG_FUNCTION_ARGS)
        ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);
        char      **t = (char **) PG_GETARG_POINTER(1);
        int                *tlen = (int *) PG_GETARG_POINTER(2);
+       int                     startpos = pst->pos;
        int                     type;
 
-       *tlen = pst->pos;
        *t = pst->buffer + pst->pos;
 
-       if ((pst->buffer)[pst->pos] == ' ')
+       if (pst->pos < pst->len &&
+               (pst->buffer)[pst->pos] == ' ')
        {
                /* blank type */
                type = 12;
-               /* go to the next non-white-space character */
-               while ((pst->buffer)[pst->pos] == ' ' &&
-                          pst->pos < pst->len)
+               /* go to the next non-space character */
+               while (pst->pos < pst->len &&
+                          (pst->buffer)[pst->pos] == ' ')
                        (pst->pos)++;
        }
        else
        {
                /* word type */
                type = 3;
-               /* go to the next white-space character */
-               while ((pst->buffer)[pst->pos] != ' ' &&
-                          pst->pos < pst->len)
+               /* go to the next space character */
+               while (pst->pos < pst->len &&
+                          (pst->buffer)[pst->pos] != ' ')
                        (pst->pos)++;
        }
 
-       *tlen = pst->pos - *tlen;
+       *tlen = pst->pos - startpos;
 
        /* we are finished if (*tlen == 0) */
        if (*tlen == 0)