libUnihan  0.5.3
Data Structures | Functions
str_functions.h File Reference

String processing functions. More...

#include <string.h>
#include <glib.h>

Go to the source code of this file.

Data Structures

struct  StringList
 StringList is a structure that stores a list of constant strings. More...
 

Functions

StringListstringList_new ()
 Create a new StringList instance. More...
 
StringListstringList_sized_new (size_t chunk_size, size_t element_count, size_t const_count)
 Create a new StringList instance with given sizes. More...
 
void stringList_clear (StringList *sList)
 Clear all content of Stringlist. More...
 
int stringList_find_string (StringList *sList, const char *str)
 Find a string in StringList. More...
 
char ** stringList_to_charPointerPointer (StringList *sList)
 Return a char pointer pointer (char **) which points to the list of strings. More...
 
const char * stringList_index (StringList *sList, guint index)
 Return the string at the given index. More...
 
guint stringList_insert (StringList *sList, const char *str)
 Insert a string to StringList. More...
 
guint stringList_insert_const (StringList *sList, const char *str)
 Insert a constant string to StringList. More...
 
void stringList_free (StringList *sList)
 Free the StringList instance. More...
 
char * initString (char *str)
 Initialize the string by setting the first char to 0x0. More...
 
gboolean isEmptyString (const char *str)
 Check whether the string is NULL or have 0 length. More...
 
void string_trim (char *str)
 Trim the leading and trailing whitespace of the string. More...
 
char * subString (char *buf, const char *str, int beginIndex, int length)
 Returns a substring of the given string. More...
 
char * ucs4_to_utf8 (gunichar ucs4_code)
 Convert UCS-4 to UTF-8 string. More...
 
gunichar * utf8_to_ucs4 (const char *utf8_str)
 Convert UTF-8 string to UCS-4 (gunichar). More...
 
char * utf8_concat_ucs4 (char *utf8_str, gunichar ucs4_code)
 Concatenate a UCS-4 (gunichar) to an UTF-8 string. More...
 
int strcmp_unsigned_signed (const unsigned char *str1, const char *str2)
 Compare between signed and unsigned char arrays. More...
 
unsigned char * signedStr_to_unsignedStr (const char *str)
 Convert the signed char string to a new allocated unsigned char string. More...
 
unsigned char * signedStr_to_unsignedStr_buffer (unsigned char *resultBuf, const char *str)
 Convert the signed char string to the unsigned char string buffer. More...
 
char * unsignedStr_to_signedStr (const unsigned char *str)
 Convert the unsigned char string to a new allocated signed char string. More...
 
char * unsignedStr_to_signedStr_buffer (char *resultBuf, const unsigned char *str)
 Convert the unsigned char string to the signed char string buffer. More...
 

Detailed Description

This header file lists the some string processing functions. Such as subString, and StringList, which provides a memory efficient methods to store a list of constrant strings.

Function Documentation

char* initString ( char *  str)

If str is NULL, then an char array with MAX_STRING_BUFFER_SIZE will be assined.

Parameters
strString to be initialize, NULL for allocate a new string..
Returns
The initialized string.
gboolean isEmptyString ( const char *  str)
Parameters
strString to be check.
Returns
False if the string is not empty, true otherwise.
unsigned char* signedStr_to_unsignedStr ( const char *  str)
Parameters
strSigned char string.
Returns
A new allocated unsigned char string.
See also
signedStr_to_unsignedStr_buffer()
unsignedStr_to_signedStr()
unsignedStr_to_signedStr_buffer()
unsigned char* signedStr_to_unsignedStr_buffer ( unsigned char *  resultBuf,
const char *  str 
)
Parameters
resultBufThe buffer that stored the conversion result.
strSigned char string.
See also
signedStr_to_unsignedStr()
unsignedStr_to_signedStr()
unsignedStr_to_signedStr_buffer()
int strcmp_unsigned_signed ( const unsigned char *  str1,
const char *  str2 
)

It behaves like strcmp() except the comparison is between a unsigned string (char array) and signed string. Mainly for GCC 4.3

Parameters
str1Unsigned string to be compared.
str2Signed string to be compared.
Returns
An integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.
void string_trim ( char *  str)

Note the content of str might be changed. Use strdup() or g_strdup() to backup.

Parameters
strString to be trim.
void stringList_clear ( StringList sList)
Parameters
sListThe StringList to be processed.
int stringList_find_string ( StringList sList,
const char *  str 
)

If found, this function returns the index of the string from 0, otherwise returns -1.

Parameters
sListThe StringList to be processed.
strThe character to be found.
Returns
The index of the string from 0 if found; -1 otherwise.
void stringList_free ( StringList sList)

Note that this function assumes the sList is not NULL. Use if (sList) stringList_free(sList); to tolerate the NULL parameter.

Parameters
sListThe StringList to be processed.
const char* stringList_index ( StringList sList,
guint  index 
)
Parameters
sListThe StringList to be processed.
indexThe given index.
Returns
The string at the given index.
guint stringList_insert ( StringList sList,
const char *  str 
)

This functions copies the str to the string list. It behaves like g_string_chunk_insert(), that is, it does not check for the duplicates. However, it returns the index instead of char pointer of inserted string.

The difference between this function and stringList_insert_const() is that each inserted identical string will have it own spaces.

Parameters
sListThe StringList to be processed.
strString to be inserted, can be NULL.
Returns
the index of the newly inserted string.
See also
stringList_insert_const()
guint stringList_insert_const ( StringList sList,
const char *  str 
)

This functions copies the str to the string list. It behaves like g_string_chunk_insert_const(), that is, it checks for the duplicates. However, it returns the index instead of char pointer of inserted string.

The difference between this function and stringList_insert() is that each inserted identical string will share the same space.

Parameters
sListThe StringList to be processed.
strString to be inserted.
Returns
the index of the newly inserted string.
See also
stringList_insert()
StringList* stringList_new ( )
Returns
the pointer to the allocated space.
StringList* stringList_sized_new ( size_t  chunk_size,
size_t  element_count,
size_t  const_count 
)

This function allocate space for a StringList with given size, thus avoid frequent reallocation.

Parameters
chunk_sizeSize in bytes required for string storage.
element_countNumber of strings.
const_countNumber of constant strings (strings without duplication).
Returns
the pointer to the allocated space.
char** stringList_to_charPointerPointer ( StringList sList)

This function returns a char** which points to the places that strings are stored. The pointer directly points to content in StringList instance, so the returned pointer should not be free.

Use the stringList_free to free instead.

Parameters
sListThe StringList to be processed.
Returns
The index of the string from 0 if found; -1 otherwise.
char* subString ( char *  buf,
const char *  str,
int  beginIndex,
int  length 
)

The substring begins at the specified beginIndex and end after length bytes. The index starts from zero.

Parameters
bufbuffer that stores the result.
strString to be process
beginIndexthe beginning index, inclusive.
lengthtotal bytes to copy.
Returns
The specified substring.
char* ucs4_to_utf8 ( gunichar  ucs4_code)
Parameters
ucs4_codethe UCS-4 to be converted.
Returns
UTF-8 string that converted from the UCS-4 Code. Use g_free() after use.
char* unsignedStr_to_signedStr ( const unsigned char *  str)
Parameters
strUnsigned char string.
Returns
A new allocated signed char string.
See also
signedStr_to_unsignedStr()
signedStr_to_unsignedStr_buffer()
unsignedStr_to_signedStr_buffer()
char* unsignedStr_to_signedStr_buffer ( char *  resultBuf,
const unsigned char *  str 
)
Parameters
resultBufThe buffer that stored the conversion result.
strUnsigned char string.
See also
signedStr_to_unsignedStr()
signedStr_to_unsignedStr_buffer()
unsignedStr_to_signedStr()
char* utf8_concat_ucs4 ( char *  utf8_str,
gunichar  ucs4_code 
)
Parameters
utf8_strthe UTF-8 string.
ucs4_codethe UCS-4 to be appended.
Returns
a pointer to utf8_str;
gunichar* utf8_to_ucs4 ( const char *  utf8_str)
Parameters
utf8_strthe UTF-8 string to be converted.
Returns
UCS-4 representation of the UTF-8 string.