Functions
string.c File Reference

Functions for manipulating strings. More...

#include <GKlib.h>
Include dependency graph for string.c:

Go to the source code of this file.

Functions

int gk_GetStringID (gk_StringMap_t *strmap, char *key)
 
time_t gk_str2time (char *str)
 Converts a date/time string into its equivalent time_t value. More...
 
int gk_strcasecmp (char *s1, char *s2)
 Case insensitive string comparison. More...
 
char * gk_strchr_replace (char *str, char *fromlist, char *tolist)
 Replaces certain characters in a string. More...
 
char * gk_strdup (char *orgstr)
 Duplicates a string. More...
 
char * gk_strhprune (char *str, char *rmlist)
 Prunes characters from the beginning of the string. More...
 
int gk_strrcmp (char *s1, char *s2)
 Compare two strings in revere order. More...
 
int gk_strstr_replace (char *str, char *pattern, char *replacement, char *options, char **new_str)
 Regex-based search-and-replace function. More...
 
char * gk_strtolower (char *str)
 Converts a string to lower case. More...
 
char * gk_strtoupper (char *str)
 Converts a string to upper case. More...
 
char * gk_strtprune (char *str, char *rmlist)
 Prunes characters from the end of the string. More...
 
char * gk_time2str (time_t time)
 Converts a time_t time into a string. More...
 

Detailed Description

Functions for manipulating strings.

Various functions for manipulating strings. Some of these functions provide new functionality, whereas others are drop-in replacements of standard functions (but with enhanced functionality).

Date
Started 11/1/99
Author
George
Version
Id
string.c 10711 2011-08-31 22:23:04Z karypis

Definition in file string.c.

Function Documentation

◆ gk_GetStringID()

int gk_GetStringID ( gk_StringMap_t strmap,
char *  key 
)

Definition at line 519 of file string.c.

◆ gk_str2time()

time_t gk_str2time ( char *  str)

Converts a date/time string into its equivalent time_t value.

This function takes date and/or time specification and converts it in the equivalent time_t representation. The conversion is done using the strptime() function. The format that gk_str2time() understands is mm/dd/yyyy hh:mm:ss, in which the hours are in military time.

Parameters
stris the date/time string to be converted.
Returns
If the conversion was successful it returns the time, otherwise it returns -1.

Definition at line 499 of file string.c.

◆ gk_strcasecmp()

int gk_strcasecmp ( char *  s1,
char *  s2 
)

Case insensitive string comparison.

This function compares two strings for equality by ignoring the case of the strings.

Warning
This function is not equivalent to a case-insensitive strcmp() function, as it does not return ordering information.
Todo:
Remove the above warning.
Parameters
s1is the first string to be compared.
s2is the second string to be compared.
Return values
1if the strings are identical,
0otherwise.

Definition at line 405 of file string.c.

◆ gk_strchr_replace()

char* gk_strchr_replace ( char *  str,
char *  fromlist,
char *  tolist 
)

Replaces certain characters in a string.

This function takes a string and replaces all the characters in the fromlist with the corresponding characters from the tolist. That is, each occurence of fromlist[i] is replaced by tolist[i]. If the tolist is shorter than fromlist, then the corresponding characters are deleted. The modifications on str are done in place. It tries to provide a functionality similar to Perl's tr// function.

Parameters
stris the string whose characters will be replaced.
fromlistis the set of characters to be replaced.
tolistis the set of replacement characters .
Returns
A pointer to str itself.

Definition at line 37 of file string.c.

◆ gk_strdup()

char* gk_strdup ( char *  orgstr)

Duplicates a string.

This function is a replacement for C's standard strdup() function. The key differences between the two are that gk_strdup():

  • uses the dynamic memory allocation routines of GKlib.
  • it correctly handles NULL input strings.

The string that is returned must be freed by gk_free().

Parameters
orgstris the string that will be duplicated.
Returns
A pointer to the newly created string.
See also
gk_free()

Definition at line 372 of file string.c.

◆ gk_strhprune()

char* gk_strhprune ( char *  str,
char *  rmlist 
)

Prunes characters from the beginning of the string.

This function removes any starting characters that are included in the rmlist. The trimming stops at the first character that is not in rmlist. This function can be used to removed leading spaces, tabs, etc. This is a distructive operation as it modifies the string.

Parameters
stris the string that will be trimmed.
rmlistcontains the set of characters that will be removed.
Returns
A pointer to str itself.
See also
gk_strtprune()

Definition at line 291 of file string.c.

◆ gk_strrcmp()

int gk_strrcmp ( char *  s1,
char *  s2 
)

Compare two strings in revere order.

This function is similar to strcmp but it performs the comparison as if the two strings were reversed.

Parameters
s1is the first string to be compared.
s2is the second string to be compared.
Return values
-1,0,1,ifthe s1 < s2, s1 == s2, or s1 > s2.

Definition at line 433 of file string.c.

◆ gk_strstr_replace()

int gk_strstr_replace ( char *  str,
char *  pattern,
char *  replacement,
char *  options,
char **  new_str 
)

Regex-based search-and-replace function.

This function is a C implementation of Perl's s// regular-expression based substitution function.

Parameters
stris the input string on which the operation will be performed.
patternis the regular expression for the pattern to be matched for substitution.
replacementis the replacement string, in which the possible captured pattern substrings are referred to as $1, $2, ..., $9. The entire matched pattern is refered to as $0.
optionsis a string specified options for the substitution operation. Currently the "i" (case insensitive) and "g" (global substitution) are supported.
new_stris a reference to a pointer that will store a pointer to the newly created string that results from the substitutions. This string is allocated via gk_malloc() and needs to be freed using gk_free(). The string is returned even if no substitutions were performed.
Returns
If successful, it returns 1 + the number of substitutions that were performed. Thus, if no substitutions were performed, the returned value will be 1. Otherwise it returns 0. In case of error, a meaningful error message is returned in newstr, which also needs to be freed afterwards.

Definition at line 94 of file string.c.

◆ gk_strtolower()

char* gk_strtolower ( char *  str)

Converts a string to lower case.

This function converts a string to lower case. This operation modifies the string itself.

Parameters
stris the string whose case will be changed.
Returns
A pointer to str itself.
See also
gk_strtoupper()

Definition at line 348 of file string.c.

◆ gk_strtoupper()

char* gk_strtoupper ( char *  str)

Converts a string to upper case.

This function converts a string to upper case. This operation modifies the string itself.

Parameters
stris the string whose case will be changed.
Returns
A pointer to str itself.
See also
gk_strtolower()

Definition at line 328 of file string.c.

◆ gk_strtprune()

char* gk_strtprune ( char *  str,
char *  rmlist 
)

Prunes characters from the end of the string.

This function removes any trailing characters that are included in the rmlist. The trimming stops at the last character (i.e., first character from the end) that is not in rmlist. This function can be used to removed trailing spaces, newlines, etc. This is a distructive operation as it modifies the string.

Parameters
stris the string that will be trimmed.
rmlistcontains the set of characters that will be removed.
Returns
A pointer to str itself.
See also
gk_strhprune()

Definition at line 254 of file string.c.

◆ gk_time2str()

char* gk_time2str ( time_t  time)

Converts a time_t time into a string.

This function takes a time_t-specified time and returns a string-formated representation of the corresponding time. The format of the string is mm/dd/yyyy hh:mm:ss, in which the hours are in military time.

Parameters
timeis the time to be converted.
Returns
It returns a pointer to a statically allocated string that is over-written in successive calls of this function. If the conversion failed, it returns NULL.

Definition at line 470 of file string.c.



gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:54