Back to index

libcitadel  8.12
Modules | Functions
Stringbuffer, A class for manipulating strings with dynamic buffers

StrBuf is a versatile class, aiding the handling of dynamic strings. More...

Collaboration diagram for Stringbuffer, A class for manipulating strings with dynamic buffers:

Modules

 Create/Destroy StrBufs
 Cast operators to interact with char based code
 

use these operators to interfere with code demanding char*; if you need to own the content, smash me.


 Create/Replace/Append Content into a StrBuf
 

operations to get your Strings into a StrBuf, manipulating them, or appending


 Fast tokenizer to pull tokens in sequence
 

Quick tokenizer; demands of the user to pull its tokens in sequence.


 tokenizer Functions; Slow ones.
 

versatile tokenizer; random access to tokens, but slower; Prefer the Next Tokenizer


 Buffered IO with Asynchroneous reads and no unneeded memmoves (the fast ones)
 

File IO to fill StrBufs; Works with work-buffer shared across several calls; External Cursor to maintain the current read position inside of the buffer the non-fast ones will use memove to keep the start of the buffer the read buffer (which is slower)


 FileIO; Prefer @ref StrBuf_BufferedIO
 

Slow I/O; avoid.


 functions to translate the contents of a buffer
 

these functions translate the content of a buffer into another representation; some are combined Fillers and encoders


Functions

static void SwapBuffers (StrBuf *A, StrBuf *B)
 swaps the contents of two StrBufs this is to be used to have cheap switched between a work-buffer and a target buffer
long StrTol (const StrBuf *Buf)
 Wrapper around atol.
int StrToi (const StrBuf *Buf)
 Wrapper around atoi.
int StrBufIsNumber (const StrBuf *Buf)
 Checks to see if the string is a pure number.
int StrBufSub (StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
 extracts a substring from Source into dest
void StrBufCutLeft (StrBuf *Buf, int nChars)
 Cut nChars from the start of the string.
void StrBufCutRight (StrBuf *Buf, int nChars)
 Cut the trailing n Chars from the string.
void StrBufCutAt (StrBuf *Buf, int AfternChars, const char *At)
 Cut the string after n Chars.
void StrBufTrim (StrBuf *Buf)
 Strip leading and trailing spaces from a string; with premeasured and adjusted length.
void StrBufSpaceToBlank (StrBuf *Buf)
 changes all spaces in the string (tab, linefeed...) to Blank (0x20)
void StrBufReplaceChars (StrBuf *buf, char search, char replace)
 replaces all occurances of 'search' by 'replace'
void StrBufToUnixLF (StrBuf *buf)
 removes all \r s from the string, or replaces them with
if its not a combination of both.
static int Ctdl_GetUtf8SequenceLength (const char *CharS, const char *CharE)
 evaluate the length of an utf8 special character sequence
static int Ctdl_IsUtf8SequenceStart (const char Char)
 detect whether this char starts an utf-8 encoded char
long StrBuf_Utf8StrLen (StrBuf *Buf)
 measure the number of glyphs in an UTF8 string...
long StrBuf_Utf8StrCut (StrBuf *Buf, int maxlen)
 cuts a string after maxlen glyphs

Detailed Description

StrBuf is a versatile class, aiding the handling of dynamic strings.


Function Documentation

static int Ctdl_GetUtf8SequenceLength ( const char *  CharS,
const char *  CharE 
) [inline, static]

evaluate the length of an utf8 special character sequence

Parameters:
Charthe character to examine
Returns:
width of utf8 chars in bytes; if the sequence is broken 0 is returned; 1 if its simply ASCII.

Definition at line 3429 of file stringbuf.c.

{
       int n = 0;
        unsigned char test = (1<<7);

       if ((*CharS & 0xC0) != 0xC0) 
              return 1;

       while ((n < 8) && 
              ((test & ((unsigned char)*CharS)) != 0)) 
       {
              test = test >> 1;
              n ++;
       }
       if ((n > 6) || ((CharE - CharS) < n))
              n = 0;
       return n;
}

Here is the caller graph for this function:

static int Ctdl_IsUtf8SequenceStart ( const char  Char) [inline, static]

detect whether this char starts an utf-8 encoded char

Parameters:
Charcharacter to inspect
Returns:
yes or no

11??.???? indicates an UTF8 Sequence.

Definition at line 3454 of file stringbuf.c.

{
       return ((Char & 0xC0) == 0xC0);
}

Here is the caller graph for this function:

long StrBuf_Utf8StrCut ( StrBuf Buf,
int  maxlen 
)

cuts a string after maxlen glyphs

Parameters:
Bufstring to cut to maxlen glyphs
maxlenhow long may the string become?
Returns:
current length of the string

Definition at line 3497 of file stringbuf.c.

{
       char *aptr, *eptr;
       int n = 0, m = 0;

       aptr = Buf->buf;
       eptr = Buf->buf + Buf->BufUsed;
       while ((aptr < eptr) && (*aptr != '\0')) {
              if (Ctdl_IsUtf8SequenceStart(*aptr)){
                     m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
                     while ((*aptr++ != '\0') && (m-- > 0));
                     n ++;
              }
              else {
                     n++;
                     aptr++;
              }
              if (n > maxlen) {
                     *aptr = '\0';
                     Buf->BufUsed = aptr - Buf->buf;
                     return Buf->BufUsed;
              }                    
       }
       return Buf->BufUsed;

}

Here is the call graph for this function:

long StrBuf_Utf8StrLen ( StrBuf Buf)

measure the number of glyphs in an UTF8 string...

Parameters:
Bufstring to measure
Returns:
the number of glyphs in Buf

Definition at line 3466 of file stringbuf.c.

{
       int n = 0;
       int m = 0;
       char *aptr, *eptr;

       if ((Buf == NULL) || (Buf->BufUsed == 0))
              return 0;
       aptr = Buf->buf;
       eptr = Buf->buf + Buf->BufUsed;
       while ((aptr < eptr) && (*aptr != '\0')) {
              if (Ctdl_IsUtf8SequenceStart(*aptr)){
                     m = Ctdl_GetUtf8SequenceLength(aptr, eptr);
                     while ((aptr < eptr) && (*aptr++ != '\0')&& (m-- > 0) );
                     n ++;
              }
              else {
                     n++;
                     aptr++;
              }
       }
       return n;
}

Here is the call graph for this function:

void StrBufCutAt ( StrBuf Buf,
int  AfternChars,
const char *  At 
)

Cut the string after n Chars.

Parameters:
BufBuffer to modify
AfternCharsafter how many chars should we trunkate the string?
Atif non-null and points inside of our string, cut it there.

Definition at line 1085 of file stringbuf.c.

{
       if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
       if (At != NULL){
              AfternChars = At - Buf->buf;
       }

       if ((AfternChars < 0) || (AfternChars >= Buf->BufUsed))
              return;
       Buf->BufUsed = AfternChars;
       Buf->buf[Buf->BufUsed] = '\0';
}

Here is the caller graph for this function:

void StrBufCutLeft ( StrBuf Buf,
int  nChars 
)

Cut nChars from the start of the string.

Parameters:
BufBuffer to modify
nCharshow many chars should be skipped?

Definition at line 1047 of file stringbuf.c.

{
       if ((Buf == NULL) || (Buf->BufUsed == 0)) return;
       if (nChars >= Buf->BufUsed) {
              FlushStrBuf(Buf);
              return;
       }
       memmove(Buf->buf, Buf->buf + nChars, Buf->BufUsed - nChars);
       Buf->BufUsed -= nChars;
       Buf->buf[Buf->BufUsed] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

void StrBufCutRight ( StrBuf Buf,
int  nChars 
)

Cut the trailing n Chars from the string.

Parameters:
BufBuffer to modify
nCharshow many chars should be trunkated?

Definition at line 1065 of file stringbuf.c.

{
       if ((Buf == NULL) || (Buf->BufUsed == 0) || (Buf->buf == NULL))
              return;

       if (nChars >= Buf->BufUsed) {
              FlushStrBuf(Buf);
              return;
       }
       Buf->BufUsed -= nChars;
       Buf->buf[Buf->BufUsed] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

int StrBufIsNumber ( const StrBuf Buf)

Checks to see if the string is a pure number.

Parameters:
BufThe buffer to inspect
Returns:
1 if its a pure number, 0, if not.

Definition at line 747 of file stringbuf.c.

                                      {
       char * pEnd;
       if ((Buf == NULL) || (Buf->BufUsed == 0)) {
              return 0;
       }
       strtoll(Buf->buf, &pEnd, 10);
       if (pEnd == Buf->buf)
              return 0;
       if ((pEnd != NULL) && (pEnd == Buf->buf + Buf->BufUsed))
              return 1;
       if (Buf->buf == pEnd)
              return 0;
       return 0;
} 

Here is the caller graph for this function:

void StrBufReplaceChars ( StrBuf buf,
char  search,
char  replace 
)

replaces all occurances of 'search' by 'replace'

Parameters:
bufBuffer to modify
searchcharacter to search
replacecharacter to replace search by

Definition at line 2979 of file stringbuf.c.

{
       long i;
       if (buf == NULL)
              return;
       for (i=0; i<buf->BufUsed; i++)
              if (buf->buf[i] == search)
                     buf->buf[i] = replace;

}
void StrBufSpaceToBlank ( StrBuf Buf)

changes all spaces in the string (tab, linefeed...) to Blank (0x20)

Parameters:
Bufthe string to modify

Definition at line 1128 of file stringbuf.c.

{
       char *pche, *pch;

       if ((Buf == NULL) || (Buf->BufUsed == 0)) return;

       pch = Buf->buf;
       pche = pch + Buf->BufUsed;
       while (pch < pche) 
       {
              if (isspace(*pch))
                     *pch = ' ';
              pch ++;
       }
}
int StrBufSub ( StrBuf dest,
const StrBuf Source,
unsigned long  Offset,
size_t  nChars 
)

extracts a substring from Source into dest

Parameters:
destbuffer to place substring into
Sourcestring to copy substring from
Offsetchars to skip from start
nCharsnumber of chars to copy
Returns:
the number of chars copied; may be different from nChars due to the size of Source

Definition at line 1014 of file stringbuf.c.

{
       size_t NCharsRemain;
       if (Offset > Source->BufUsed)
       {
              if (dest != NULL)
                     FlushStrBuf(dest);
              return 0;
       }
       if (Offset + nChars < Source->BufUsed)
       {
              if (nChars >= dest->BufSize)
                     IncreaseBuf(dest, 0, nChars + 1);
              memcpy(dest->buf, Source->buf + Offset, nChars);
              dest->BufUsed = nChars;
              dest->buf[dest->BufUsed] = '\0';
              return nChars;
       }
       NCharsRemain = Source->BufUsed - Offset;
       if (NCharsRemain  >= dest->BufSize)
              IncreaseBuf(dest, 0, NCharsRemain + 1);
       memcpy(dest->buf, Source->buf + Offset, NCharsRemain);
       dest->BufUsed = NCharsRemain;
       dest->buf[dest->BufUsed] = '\0';
       return NCharsRemain;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void StrBufToUnixLF ( StrBuf buf)

removes all \r s from the string, or replaces them with
if its not a combination of both.

Parameters:
bufBuffer to modify

Definition at line 2995 of file stringbuf.c.

{
       char *pche, *pchS, *pchT;
       if (buf == NULL)
              return;

       pche = buf->buf + buf->BufUsed;
       pchS = pchT = buf->buf;
       while (pchS < pche)
       {
              if (*pchS == '\r')
              {
                     pchS ++;
                     if (*pchS != '\n') {
                            *pchT = '\n';
                            pchT++;
                     }
              }
              *pchT = *pchS;
              pchT++; pchS++;
       }
       *pchT = '\0';
       buf->BufUsed = pchT - buf->buf;
}
void StrBufTrim ( StrBuf Buf)

Strip leading and trailing spaces from a string; with premeasured and adjusted length.

Parameters:
Bufthe string to modify

Definition at line 1104 of file stringbuf.c.

{
       int delta = 0;
       if ((Buf == NULL) || (Buf->BufUsed == 0)) return;

       while ((Buf->BufUsed > 0) &&
              isspace(Buf->buf[Buf->BufUsed - 1]))
       {
              Buf->BufUsed --;
       }
       Buf->buf[Buf->BufUsed] = '\0';

       if (Buf->BufUsed == 0) return;

       while ((Buf->BufUsed > delta) && (isspace(Buf->buf[delta]))){
              delta ++;
       }
       if (delta > 0) StrBufCutLeft(Buf, delta);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int StrToi ( const StrBuf Buf)

Wrapper around atoi.

Definition at line 731 of file stringbuf.c.

{
       if (Buf == NULL)
              return 0;
       if (Buf->BufUsed > 0)
              return atoi(Buf->buf);
       else
              return 0;
}

Here is the caller graph for this function:

long StrTol ( const StrBuf Buf)

Wrapper around atol.

Definition at line 717 of file stringbuf.c.

{
       if (Buf == NULL)
              return 0;
       if(Buf->BufUsed > 0)
              return atol(Buf->buf);
       else
              return 0;
}

Here is the caller graph for this function:

static void SwapBuffers ( StrBuf A,
StrBuf B 
) [inline, static]

swaps the contents of two StrBufs this is to be used to have cheap switched between a work-buffer and a target buffer

Parameters:
AFirst one
Bsecond one

Definition at line 249 of file stringbuf.c.

{
       StrBuf C;

       memcpy(&C, A, sizeof(*A));
       memcpy(A, B, sizeof(*B));
       memcpy(B, &C, sizeof(C));

}

Here is the caller graph for this function: