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

#include <stdlib.h>
#include <assert.h>

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

int main(int argc, char *argv[])
{
  String s,t,w;
  int    d;
  ulong  nextIndex;

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

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

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

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

  /* 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\n",
                123,
                456L,
                789LL,
                0.123,
                String_cString(s),
                s,
                String_cString(t),
                t
               );  
  printf("s=%s\n",String_cString(s));
  printf("\n");
  String_delete(t);
  String_delete(s);

  /* parse string */
  s = String_new();
  t = String_new();
  w = String_new();
  String_setCString(s,"Hello 4711 we are 08.15");
  if (String_parse(s,STRING_BEGIN,"%S %d % S",NULL,t,&d,w))
  {
    printf("Parse result 1:\n");
    printf("t=%s\n",String_cString(t));
    printf("d=%d\n",d);
    printf("w=%s\n",String_cString(w));
    printf("\n");
  }
  if (String_parse(s,STRING_BEGIN,"Hello ",&nextIndex))
  {
    printf("Parse result 2:\n");
    printf("next index=%lu\n",nextIndex);
    printf("\n");
  }
  String_delete(w);
  String_delete(t);
  String_delete(s);

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

  String_delete(w);
  String_delete(t);
  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();
  String_debugPrintStatistics();

  return 0;
}

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 -o strings_demo strings_demo.c strings.o lists.o global.o -lpthread
Please note:

License

String library is currently under GPL version 2.

Download

strings-0.08.tar.bz2

ChangeLog

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