Home Download Change log

Introduction

String handling in standard C is a painful thing. To avoid this I implemented this C dynamic string library. It is written in standard C and kept as simple as possible, but also flexibel in usage. If you are looking for functions to process strings, feel free to use the library.

Features

Examples

/***********************************************************************\
*
* $Source$
* $Revision: 940 $
* $Author: torsten $
* Contents: dynamic strings demo
* Systems: *nix
*
\***********************************************************************/

/****************************** Includes *******************************/
#include 
#include 
#include 

#include "global.h"
#include "strings.h"

/****************** Conditional compilation switches *******************/

/***************************** Constants *******************************/

/***************************** Datatypes *******************************/

/***************************** Variables *******************************/

/****************************** Macros *********************************/

/***************************** Forwards ********************************/

/***************************** Functions *******************************/

#ifdef __cplusplus
  extern "C" {
#endif

/*---------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
  String          s,t,w;
  int             i,j;
  long            nextIndex;
  StringTokenizer stringTokenizer;

  // allocate and free a string
  s = String_new();
  String_delete(s);

  s = String_newCString("Hello world!");
  String_delete(s);

  // assign string, append string
  s = String_new();
  String_setCString(s,"Hello world, ");
  String_appendCString(s,"I'm here!");
  String_delete(s);

  // print string
  s = String_newCString("Hello world!\n");
  printf("%s\n",String_cString(s));
  String_delete(s);
  printf("\n");

  // string length, sub-string, find string
  s = String_new();
  String_setCString(s,"Hello world, I'm here!");
  printf("string length '%s': %lu characters\n",String_cString(s),String_length(s));
  t = String_sub(String_new(),s,0,5);
  printf("sub-string %s",String_cString(t));
  printf("find string 'world': index %ld\n",String_findCString(s,STRING_BEGIN,"world"));
  String_delete(t);
  String_delete(s);
  printf("\n");

  // format string
  s = String_new();
  t = String_new();
  String_setCString(s,"Hello");
  String_setCString(t,"world");
  printf("Format result:\n");
  String_format(s,
                "%d %ld %lld %f %s %S %'s %'S",
                123,
                456L,
                789LL,
                0.123,
                String_cString(s),
                s,
                String_cString(t),
                t
               );
  printf("s=%s\n",String_cString(s));
  String_delete(t);
  String_delete(s);
  printf("\n");

  // parse string
  s = String_new();
  t = String_new();
  w = String_new();
  String_setCString(s,"Hello 4711 we are 08.15");
  printf("String: %s\n",String_cString(s));
  if (String_parse(s,STRING_BEGIN,"%S %d % S",NULL,t,&i,w))
  {
    printf("Parse result 1:\n");
    printf("string=%s\n",String_cString(t));
    printf("int=%d\n",i);
    printf("rest=%s\n",String_cString(w));
  }
  if (String_parse(s,STRING_BEGIN,"Hello ",&nextIndex))
  {
    printf("Parse result 2:\n");
    printf("next index after 'Hello '=%lu\n",nextIndex);
  }
  String_setCString(s,"foo 1");
  printf("String: %s\n",String_cString(s));
  if (String_parse(s,STRING_BEGIN,"foo %d %d",NULL,&i,&j))
  {
    printf("Parse result 3a:\n");
    printf("i=%d\n",i);
    printf("j=%d\n",j);
  }
  if (String_parse(s,STRING_BEGIN,"foo %d",NULL,&i))
  {
    printf("Parse result 3b:\n");
    printf("i=%d\n",i);
  }
  String_setCString(s,"foo/");
  printf("String: %s\n",String_cString(s));
  if (String_parse(s,STRING_BEGIN,"%S/%S",&nextIndex,t,w))
  {
    printf("Parse result 4:\n");
    printf("next index=%ld=%s\n",nextIndex,(nextIndex == STRING_END) ? "none" : "some");
  }
  String_delete(w);
  String_delete(t);
  String_delete(s);
  printf("\n");

  // match string
  s = String_new();
  t = String_new();
  w = String_new();
  String_setCString(s,"Hello 4711 we are 08.15");
  printf("String: %s\n",String_cString(s));
  if (String_matchCString(s,STRING_BEGIN,".* ([0-7]+) .*are ([[:digit:]]+).*",NULL,NULL,t,w))
  {
    printf("Match result 1:\n");
    printf("group 1 [0-7]+=%s\n",String_cString(t));
    printf("group 2 [[:digit:]]+=%s\n",String_cString(w));
  }
  printf("\n");
  String_delete(w);
  String_delete(t);
  String_delete(s);

  s = String_new();
  t = String_new();
  w = String_new();
  String_setCString(s,"it_foo.com:345");
  printf("String: %s\n",String_cString(s));
  if (String_matchCString(s,STRING_BEGIN,"[[:alnum:]_]+:[[:digit:]]+",NULL,NULL))
  {
    printf("Match result 2:\n");
    printf("match=%s\n",String_cString(s));
  }
  String_delete(w);
  String_delete(t);
  String_delete(s);
  printf("\n");

  // tokenizer
  s = String_new();
  String_setCString(s,"Hello world,, I'm here!");
  printf("String: %s\n",String_cString(s));
  String_initTokenizer(&stringTokenizer,s,STRING_BEGIN," ,",NULL,TRUE);
  while (String_getNextToken(&stringTokenizer,&t,NULL))
  {
    printf("token=%s\n",String_cString(t));
  }
  String_doneTokenizer(&stringTokenizer);

  String_setCString(s,"'Die Verwandlung' by 'Franz Kafka'!");
  printf("String: %s\n",String_cString(s));
  String_initTokenizer(&stringTokenizer,s,STRING_BEGIN," ,","'",TRUE);
  while (String_getNextToken(&stringTokenizer,&t,NULL))
  {
    printf("token=%s\n",String_cString(t));
  }
  String_doneTokenizer(&stringTokenizer);
  String_delete(s);
  printf("\n");

  // misc functions
  s = String_new();
  String_setCString(s,"  Hello 'World!'  ");
  printf("String: #%s#\n",String_cString(s));
  String_trimRight(s,STRING_WHITE_SPACES);
  printf("trim right=#%s#\n",String_cString(s));
  String_trimLeft(s,STRING_WHITE_SPACES);
  printf("trim left=#%s#\n",String_cString(s));
  String_toLower(s);
  printf("lower=#%s#\n",String_cString(s));
  String_toUpper(s);
  printf("upper=#%s#\n",String_cString(s));
  String_escape(s,"'",'\\');
  printf("escaped '=#%s#\n",String_cString(s));
  String_unescape(s,'\\');
  printf("ununescaped '=#%s#\n",String_cString(s));
  String_quote(s,'\'');
  printf("quoted=#%s#\n",String_cString(s));
  String_unquote(s,"'");
  printf("unquoted=#%s#\n",String_cString(s));
  String_delete(s);

  // uncomment to see debug functions

  #if 0
  // debug functions: lost string
  s = String_new();
  #endif /* 0 */

  #if 0
  // debug function: duplicate free
  s = String_new();
  String_delete(s);
  String_delete(s);
  #endif /* 0 */

  #if 0
  // debug function: invalid string
  printf(String_cString(s));
  String_delete(s);
  #endif /* 0 */

  String_debugPrintInfo();

  return 0;
}

#ifdef __cplusplus
  }
#endif

/* end of file */

Compile

Sorry, there is no makefile, but compilation is simple:
    
gcc -c strings.c -DHAVE_LONG_LONG
gcc -c lists.c
gcc -c global.c
gcc -c strings_demo.c
gcc -o strings_demo strings_demo.o strings.o lists.o global.o -lpthread
Please note:

License

String library is currently under GPL version 2.

Download

strings-0.10.tar.bz2

ChangeLog

2012-07-07 0.10
  * support unsigned integer types in String_parse()
  * fixed memory leak in String_[un]escape(), String_[un]quote()

2012-05-06 0.09
  * implemented String_map*()
  * support quoted strings in tokenizer
  * fixed unescape, unquote
  * fix format %s, %S
  * support NULL in match to skip matched sub-strings
  * enclose GNU specific pragam in ifdef
  * improved debug code
  * fixed warnings

2010-12-17 0.08
  * improved debugging functions

2009-02-13 0.07
  * implemented erase() function to delete critical content in
    string from memory, e. g. a password
  * implemented '*' in parse function
  * fixed formating %c

2008-12-28 0.06
  * fixed problem when partial parsing strings
  * improved example
  * fixed parsing of float/double
  * added index to String_initTokenizer

2008-12-13 0.05
  * improved matching function

2008-12-07 0.04
  * fixed parsing of float numbers starting with "."

2008-12-05 0.03
  * fixed parsing of negative numbers

2008-11-29 0.02
  * implemented String_subEquals

2008-09-26 0.01
  * initial public release
      

    
Back to top