src/ucx/string.h

Wed, 16 May 2018 19:27:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 May 2018 19:27:45 +0200
changeset 321
9af21a50b516
parent 320
0ffb71f15426
child 322
fd21d1840dff
permissions
-rw-r--r--

adds scstr_t to modules.md + fixes parenthesis bug in sstrsplit_a macro

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    28 /**
    29  * Bounded string implementation.
    30  * 
    31  * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings.
    32  * The main difference to C strings is, that <code>sstr_t</code> does <b>not
    33  * need to be <code>NULL</code>-terminated</b>. Instead the length is stored
    34  * within the structure.
    35  * 
    36  * When using <code>sstr_t</code>, developers must be full aware of what type
    37  * of string (<code>NULL</code>-terminated) or not) they are using, when 
    38  * accessing the <code>char* ptr</code> directly.
    39  * 
    40  * The UCX string module provides some common string functions, known from
    41  * standard libc, working with <code>sstr_t</code>.
    42  * 
    43  * @file   string.h
    44  * @author Mike Becker
    45  * @author Olaf Wintermann
    46  */
    48 #ifndef UCX_STRING_H
    49 #define	UCX_STRING_H
    51 #include "ucx.h"
    52 #include "allocator.h"
    53 #include <stddef.h>
    55 /** Shortcut for a <code>sstr_t struct</code> literal. */
    56 #define ST(s) { (char*)s, sizeof(s)-1 }
    58 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
    59 #define S(s) sstrn((char*)s, sizeof(s)-1)
    61 /** Expands a sstr_t or scstr_t to printf arguments. */
    62 #define SFMT(s) (int) (s).length, (s).ptr
    64 /** Format specifier for a sstr_t or scstr_t. */
    65 #define PRIsstr ".*s"
    67 #ifdef	__cplusplus
    68 extern "C" {
    69 #endif
    70 /**
    71  * The UCX string structure.
    72  */
    73 typedef struct {
    74    /** A pointer to the string
    75     * (<b>not necessarily <code>NULL</code>-terminated</b>) */
    76     char *ptr;
    77     /** The length of the string */
    78     size_t length;
    79 } sstr_t;
    81 /**
    82  * The UCX string structure for immutable (constant) strings.
    83  */
    84 typedef struct {
    85     /** A constant pointer to the immutable string
    86      * (<b>not necessarily <code>NULL</code>-terminated</b>) */
    87     const char *ptr;
    88     /** The length of the string */
    89     size_t length;
    90 } scstr_t;
    92 #ifdef	__cplusplus
    93 }
    94 #endif
    97 #ifdef __cplusplus
    98 /**
    99  * One of two type adjustment functions that return a scstr_t.
   100  * 
   101  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
   102  * 
   103  * <b>Do not use this function manually.</b>
   104  * 
   105  * @param str some sstr_t
   106  * @return an immutable (scstr_t) version of the provided string.
   107  */
   108 inline scstr_t s2scstr(sstr_t s) {
   109     scstr_t c;
   110     c.ptr = s.ptr;
   111     c.length = s.ptr;
   112     return c;
   113 }
   115 /**
   116  * One of two type adjustment functions that return a scstr_t.
   117  * 
   118  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
   119  * This variant is used, when the string is already immutable and no operation
   120  * needs to be performed.
   121  * 
   122  * <b>Do not use this function manually.</b>
   123  * 
   124  * @param str some scstr_t
   125  * @return the argument itself
   126  */
   127 inline scstr_t s2scstr(scstr_t str) {
   128     return str;
   129 }
   131 /**
   132  * Converts a UCX string to an immutable UCX string (scstr_t).
   133  * @param str some UCX string
   134  * @return the an immutable version of the provided string
   135  */
   136 #define SCSTR(s) s2scstr(s)
   137 #else
   139 /**
   140  * One of two type adjustment functions that return a scstr_t.
   141  * 
   142  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
   143  * This variant is used, when the string is already immutable and no operation
   144  * needs to be performed.
   145  * 
   146  * <b>Do not use this function manually.</b>
   147  * 
   148  * @param str some scstr_t
   149  * @return the argument itself
   150  */
   151 scstr_t ucx_sc2sc(scstr_t str);
   153 /**
   154  * One of two type adjustment functions that return a scstr_t.
   155  * 
   156  * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
   157  * 
   158  * <b>Do not use this function manually.</b>
   159  * 
   160  * @param str some sstr_t
   161  * @return an immutable (scstr_t) version of the provided string.
   162  */
   163 scstr_t ucx_ss2sc(sstr_t str);
   165 #if __STDC_VERSION__ >= 201112L
   166 /**
   167  * Converts a UCX string to an immutable UCX string (scstr_t).
   168  * @param str some UCX string
   169  * @return the an immutable version of the provided string
   170  */
   171 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
   173 #elif defined(__GNUC__) || defined(__clang__)
   175 /**
   176  * Converts a UCX string to an immutable UCX string (scstr_t).
   177  * @param str some UCX string
   178  * @return the an immutable version of the provided string
   179  */
   180 #define SCSTR(str) __builtin_choose_expr( \
   181         __builtin_types_compatible_p(typeof(str), sstr_t), \
   182         ucx_ss2sc, \
   183         ucx_sc2sc)(str)
   185 #elif defined(__sun)
   187 /**
   188  * Converts a UCX string to an immutable UCX string (scstr_t).
   189  * @param str some UCX string
   190  * @return the an immutable version of the provided string
   191  */
   192 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
   193 	scstr_t ucx_tmp_var_c; \
   194 	ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
   195 	ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
   196 	ucx_tmp_var_c; })
   197 #else /* no generics and no builtins */
   199 /**
   200  * Converts a UCX string to an immutable UCX string (scstr_t).
   201  * 
   202  * This internal function (ab)uses the C standard an expects one single
   203  * argument which is then implicitly converted to scstr_t without a warning.
   204  * 
   205  * @return the an immutable version of the provided string
   206  */
   207 scstr_t ucx_ss2c_s();
   209 /**
   210  * Converts a UCX string to an immutable UCX string (scstr_t).
   211  * @param str some UCX string
   212  * @return the an immutable version of the provided string
   213  */
   214 #define SCSTR(str) ucx_ss2c_s(str)
   215 #endif /* C11 feature test */
   217 #endif /* C++ */
   219 #ifdef	__cplusplus
   220 extern "C" {
   221 #endif
   224 /**
   225  * Creates a new sstr_t based on a C string.
   226  * 
   227  * The length is implicitly inferred by using a call to <code>strlen()</code>.
   228  *
   229  * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
   230  * do want a copy, use sstrdup() on the return value of this function.
   231  * 
   232  * If you need to wrap a constant string, use scstr().
   233  * 
   234  * @param cstring the C string to wrap
   235  * @return a new sstr_t containing the C string
   236  * 
   237  * @see sstrn()
   238  */
   239 sstr_t sstr(char *cstring);
   241 /**
   242  * Creates a new sstr_t of the specified length based on a C string.
   243  *
   244  * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
   245  * do want a copy, use sstrdup() on the return value of this function.
   246  * 
   247  * If you need to wrap a constant string, use scstrn().
   248  * 
   249  * @param cstring  the C string to wrap
   250  * @param length   the length of the string
   251  * @return a new sstr_t containing the C string
   252  * 
   253  * @see sstr()
   254  * @see S()
   255  */
   256 sstr_t sstrn(char *cstring, size_t length);
   258 /**
   259  * Creates a new scstr_t based on a constant C string.
   260  * 
   261  * The length is implicitly inferred by using a call to <code>strlen()</code>.
   262  *
   263  * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
   264  * do want a copy, use scstrdup() on the return value of this function.
   265  * 
   266  * @param cstring the C string to wrap
   267  * @return a new scstr_t containing the C string
   268  * 
   269  * @see scstrn()
   270  */
   271 scstr_t scstr(const char *cstring);
   274 /**
   275  * Creates a new scstr_t of the specified length based on a constant C string.
   276  *
   277  * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
   278  * do want a copy, use scstrdup() on the return value of this function.
   279  * 
   280  * 
   281  * @param cstring  the C string to wrap
   282  * @param length   the length of the string
   283  * @return a new scstr_t containing the C string
   284  * 
   285  * @see scstr()
   286  */
   287 scstr_t scstrn(const char *cstring, size_t length);
   289 /**
   290  * Returns the cumulated length of all specified strings.
   291  * 
   292  * <b>Attention:</b> if the count argument does not match the count of the
   293  * specified strings, the behavior is undefined.
   294  *
   295  * @param count    the total number of specified strings (so at least 1)
   296  * @param ...      all strings
   297  * @return the cumulated length of all strings
   298  */
   299 size_t scstrnlen(size_t count, ...);
   301 /**
   302  * Alias for scstrnlen() which automatically converts the arguments.
   303  * 
   304  * @param count    the total number of specified strings (so at least 1)
   305  * @param ...      all strings
   306  * @return the cumulated length of all strings
   307  */
   308 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
   310 /**
   311  * Concatenates two or more strings.
   312  * 
   313  * The resulting string will be allocated by standard <code>malloc()</code>. 
   314  * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
   315  * 
   316  * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
   317  * terminated.
   318  *
   319  * @param count   the total number of strings to concatenate
   320  * @param s1      first string
   321  * @param ...     all remaining strings
   322  * @return the concatenated string
   323  */
   324 sstr_t scstrcat(size_t count, scstr_t s1, ...);
   326 /**
   327  * Alias for scstrcat() which automatically converts the arguments.
   328  * 
   329  * @param count   the total number of strings to concatenate
   330  * @param s1      first string
   331  * @param ...     all remaining strings
   332  * @return the concatenated string
   333  */
   334 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
   336 /**
   337  * Concatenates two or more strings using a UcxAllocator.
   338  * 
   339  * See scstrcat() for details.
   340  *
   341  * @param a       the allocator to use
   342  * @param count   the total number of strings to concatenate
   343  * @param s1      first string
   344  * @param ...     all remaining strings
   345  * @return the concatenated string
   346  */
   347 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
   349 /**
   350  * Alias for scstrcat_a() which automatically converts the arguments.
   351  * 
   352  * See sstrcat() for details.
   353  *
   354  * @param a       the allocator to use
   355  * @param count   the total number of strings to concatenate
   356  * @param s1      first string
   357  * @param ...     all remaining strings
   358  * @return the concatenated string
   359  */
   360 #define sstrcat_a(a, count, s1, ...) \
   361     scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
   363 /**
   364  * Returns a substring starting at the specified location.
   365  * 
   366  * <b>Attention:</b> the new string references the same memory area as the
   367  * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
   368  * Use sstrdup() to get a copy.
   369  * 
   370  * @param string input string
   371  * @param start  start location of the substring
   372  * @return a substring of <code>string</code> starting at <code>start</code>
   373  * 
   374  * @see sstrsubsl()
   375  * @see sstrchr()
   376  */
   377 sstr_t sstrsubs(sstr_t string, size_t start);
   379 /**
   380  * Returns a substring with a maximum length starting at the specified location.
   381  * 
   382  * <b>Attention:</b> the new string references the same memory area as the
   383  * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
   384  * Use sstrdup() to get a copy.
   385  * 
   386  * @param string input string
   387  * @param start  start location of the substring
   388  * @param length the maximum length of the substring
   389  * @return a substring of <code>string</code> starting at <code>start</code>
   390  * with a maximum length of <code>length</code>
   391  * 
   392  * @see sstrsubs()
   393  * @see sstrchr()
   394  */
   395 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
   397 /**
   398  * Returns a substring of an immutable string starting at the specified
   399  * location.
   400  * 
   401  * <b>Attention:</b> the new string references the same memory area as the
   402  * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
   403  * Use scstrdup() to get a copy.
   404  * 
   405  * @param string input string
   406  * @param start  start location of the substring
   407  * @return a substring of <code>string</code> starting at <code>start</code>
   408  * 
   409  * @see scstrsubsl()
   410  * @see scstrchr()
   411  */
   412 scstr_t scstrsubs(scstr_t string, size_t start);
   414 /**
   415  * Returns a substring of an immutable string with a maximum length starting
   416  * at the specified location.
   417  * 
   418  * <b>Attention:</b> the new string references the same memory area as the
   419  * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
   420  * Use scstrdup() to get a copy.
   421  * 
   422  * @param string input string
   423  * @param start  start location of the substring
   424  * @param length the maximum length of the substring
   425  * @return a substring of <code>string</code> starting at <code>start</code>
   426  * with a maximum length of <code>length</code>
   427  * 
   428  * @see scstrsubs()
   429  * @see scstrchr()
   430  */
   431 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
   433 /**
   434  * Returns a substring starting at the location of the first occurrence of the
   435  * specified character.
   436  * 
   437  * If the string does not contain the character, an empty string is returned.
   438  * 
   439  * @param string the string where to locate the character
   440  * @param chr    the character to locate
   441  * @return       a substring starting at the first location of <code>chr</code>
   442  * 
   443  * @see sstrsubs()
   444  */
   445 sstr_t sstrchr(sstr_t string, int chr);
   447 /**
   448  * Returns a substring starting at the location of the last occurrence of the
   449  * specified character.
   450  * 
   451  * If the string does not contain the character, an empty string is returned.
   452  * 
   453  * @param string the string where to locate the character
   454  * @param chr    the character to locate
   455  * @return       a substring starting at the last location of <code>chr</code>
   456  * 
   457  * @see sstrsubs()
   458  */
   459 sstr_t sstrrchr(sstr_t string, int chr);
   461 /**
   462  * Returns an immutable substring starting at the location of the first
   463  * occurrence of the specified character.
   464  * 
   465  * If the string does not contain the character, an empty string is returned.
   466  * 
   467  * @param string the string where to locate the character
   468  * @param chr    the character to locate
   469  * @return       a substring starting at the first location of <code>chr</code>
   470  * 
   471  * @see scstrsubs()
   472  */
   473 scstr_t scstrchr(scstr_t string, int chr);
   475 /**
   476  * Returns an immutable substring starting at the location of the last
   477  * occurrence of the specified character.
   478  * 
   479  * If the string does not contain the character, an empty string is returned.
   480  * 
   481  * @param string the string where to locate the character
   482  * @param chr    the character to locate
   483  * @return       a substring starting at the last location of <code>chr</code>
   484  * 
   485  * @see scstrsubs()
   486  */
   487 scstr_t scstrrchr(scstr_t string, int chr);
   489 /**
   490  * Returns a substring starting at the location of the first occurrence of the
   491  * specified string.
   492  * 
   493  * If the string does not contain the other string, an empty string is returned.
   494  * 
   495  * If <code>match</code> is an empty string, the complete <code>string</code> is
   496  * returned.
   497  * 
   498  * @param string the string to be scanned
   499  * @param match  string containing the sequence of characters to match
   500  * @return       a substring starting at the first occurrence of
   501  *               <code>match</code>, or an empty string, if the sequence is not
   502  *               present in <code>string</code>
   503  */
   504 sstr_t scstrsstr(sstr_t string, scstr_t match);
   506 /**
   507  * Alias for scstrsstr() which automatically converts the match string.
   508  * 
   509  * @param string the string to be scanned
   510  * @param match  string containing the sequence of characters to match
   511  * @return       a substring starting at the first occurrence of
   512  *               <code>match</code>, or an empty string, if the sequence is not
   513  *               present in <code>string</code>
   514  */
   515 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
   517 /**
   518  * Returns an immutable substring starting at the location of the
   519  * first occurrence of the specified immutable string.
   520  * 
   521  * If the string does not contain the other string, an empty string is returned.
   522  * 
   523  * If <code>match</code> is an empty string, the complete <code>string</code> is
   524  * returned.
   525  * 
   526  * @param string the string to be scanned
   527  * @param match  string containing the sequence of characters to match
   528  * @return       a substring starting at the first occurrence of
   529  *               <code>match</code>, or an empty string, if the sequence is not
   530  *               present in <code>string</code>
   531  */
   532 scstr_t scstrscstr(scstr_t string, scstr_t match);
   534 /**
   535  * Alias for scstrscstr() which automatically converts the match string.
   536  * 
   537  * @param string the string to be scanned
   538  * @param match  string containing the sequence of characters to match
   539  * @return       a substring starting at the first occurrence of
   540  *               <code>match</code>, or an empty string, if the sequence is not
   541  *               present in <code>string</code>
   542  */
   543 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
   545 /**
   546  * Splits a string into parts by using a delimiter string.
   547  * 
   548  * This function will return <code>NULL</code>, if one of the following happens:
   549  * <ul>
   550  *   <li>the string length is zero</li>
   551  *   <li>the delimeter length is zero</li>
   552  *   <li>the string equals the delimeter</li>
   553  *   <li>memory allocation fails</li>
   554  * </ul>
   555  * 
   556  * The integer referenced by <code>count</code> is used as input and determines
   557  * the maximum size of the resulting array, i.e. the maximum count of splits to
   558  * perform + 1.
   559  * 
   560  * The integer referenced by <code>count</code> is also used as output and is
   561  * set to
   562  * <ul>
   563  *   <li>-2, on memory allocation errors</li>
   564  *   <li>-1, if either the string or the delimiter is an empty string</li>
   565  *   <li>0, if the string equals the delimiter</li>
   566  *   <li>1, if the string does not contain the delimiter</li>
   567  *   <li>the count of array items, otherwise</li>
   568  * </ul>
   569  * 
   570  * If the string starts with the delimiter, the first item of the resulting
   571  * array will be an empty string.
   572  * 
   573  * If the string ends with the delimiter and the maximum list size is not
   574  * exceeded, the last array item will be an empty string.
   575  * In case the list size would be exceeded, the last array item will be the
   576  * remaining string after the last split, <i>including</i> the terminating
   577  * delimiter.
   578  * 
   579  * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
   580  * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
   581  * an allocator to managed memory, to avoid this.
   582  *
   583  * @param string the string to split
   584  * @param delim  the delimiter string
   585  * @param count  IN: the maximum size of the resulting array (0 = no limit),
   586  *               OUT: the actual size of the array
   587  * @return a sstr_t array containing the split strings or
   588  * <code>NULL</code> on error
   589  * 
   590  * @see scstrsplit_a()
   591  */
   592 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
   594 /**
   595  * Alias for scstrsplit() which automatically converts the arguments.
   596  * 
   597  * @param string the string to split
   598  * @param delim  the delimiter string
   599  * @param count  IN: the maximum size of the resulting array (0 = no limit),
   600  *               OUT: the actual size of the array
   601  * @return a sstr_t array containing the split strings or
   602  * <code>NULL</code> on error
   603  * 
   604  * @see sstrsplit_a()
   605  */
   606 #define sstrsplit(string, delim, count) \
   607     scstrsplit(SCSTR(string), SCSTR(delim), count)
   609 /**
   610  * Performing scstrsplit() using a UcxAllocator.
   611  * 
   612  * <i>Read the description of scstrsplit() for details.</i>
   613  * 
   614  * The memory for the sstr_t.ptr pointers of the array items and the memory for
   615  * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
   616  * function.
   617  * 
   618  * <b>Note:</b> the allocator is not used for memory that is freed within the
   619  * same call of this function (locally scoped variables).
   620  * 
   621  * @param allocator the UcxAllocator used for allocating memory
   622  * @param string the string to split
   623  * @param delim  the delimiter string
   624  * @param count  IN: the maximum size of the resulting array (0 = no limit),
   625  *               OUT: the actual size of the array
   626  * @return a sstr_t array containing the split strings or
   627  * <code>NULL</code> on error
   628  * 
   629  * @see scstrsplit()
   630  */
   631 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
   632         ssize_t *count);
   634 /**
   635  * Alias for scstrsplit_a() which automatically converts the arguments.
   636  * 
   637  * @param allocator the UcxAllocator used for allocating memory
   638  * @param string the string to split
   639  * @param delim  the delimiter string
   640  * @param count  IN: the maximum size of the resulting array (0 = no limit),
   641  *               OUT: the actual size of the array
   642  * @return a sstr_t array containing the split strings or
   643  * <code>NULL</code> on error
   644  * 
   645  * @see sstrsplit()
   646  */
   647 #define sstrsplit_a(allocator, string, delim, count) \
   648     scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
   650 /**
   651  * Compares two UCX strings with standard <code>memcmp()</code>.
   652  * 
   653  * At first it compares the scstr_t.length attribute of the two strings. The
   654  * <code>memcmp()</code> function is called, if and only if the lengths match.
   655  * 
   656  * @param s1 the first string
   657  * @param s2 the second string
   658  * @return -1, if the length of s1 is less than the length of s2 or 1, if the 
   659  * length of s1 is greater than the length of s2 or the result of
   660  * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
   661  */
   662 int scstrcmp(scstr_t s1, scstr_t s2);
   664 /**
   665  * Alias for scstrcmp() which automatically converts its arguments.
   666  * 
   667  * @param s1 the first string
   668  * @param s2 the second string
   669  * @return -1, if the length of s1 is less than the length of s2 or 1, if the 
   670  * length of s1 is greater than the length of s2 or the result of
   671  * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
   672  */
   673 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
   675 /**
   676  * Compares two UCX strings ignoring the case.
   677  * 
   678  * At first it compares the scstr_t.length attribute of the two strings. If and
   679  * only if the lengths match, both strings are compared char by char ignoring
   680  * the case.
   681  * 
   682  * @param s1 the first string
   683  * @param s2 the second string
   684  * @return -1, if the length of s1 is less than the length of s2 or 1, if the 
   685  * length of s1 is greater than the length of s2 or the result of the platform
   686  * specific string comparison function ignoring the case.
   687  */
   688 int scstrcasecmp(scstr_t s1, scstr_t s2);
   690 /**
   691  * Alias for scstrcasecmp() which automatically converts the arguments.
   692  * 
   693  * @param s1 the first string
   694  * @param s2 the second string
   695  * @return -1, if the length of s1 is less than the length of s2 or 1, if the 
   696  * length of s1 is greater than the length of s2 or the result of the platform
   697  * specific string comparison function ignoring the case.
   698  */
   699 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
   701 /**
   702  * Creates a duplicate of the specified string.
   703  * 
   704  * The new sstr_t will contain a copy allocated by standard
   705  * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
   706  * <code>free()</code>.
   707  * 
   708  * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
   709  * terminated and mutable, regardless of the argument.
   710  * 
   711  * @param string the string to duplicate
   712  * @return a duplicate of the string
   713  * @see scstrdup_a()
   714  */
   715 sstr_t scstrdup(scstr_t string);
   717 /**
   718  * Alias for scstrdup() which automatically converts the argument.
   719  * 
   720  * @param string the string to duplicate
   721  * @return a duplicate of the string
   722  * @see sstrdup_a()
   723  */
   724 #define sstrdup(string) scstrdup(SCSTR(string))
   726 /**
   727  * Creates a duplicate of the specified string using a UcxAllocator.
   728  * 
   729  * The new sstr_t will contain a copy allocated by the allocators
   730  * UcxAllocator.malloc() function. So it is implementation depended, whether the
   731  * returned sstr_t.ptr pointer must be passed to the allocators
   732  * UcxAllocator.free() function manually.
   733  * 
   734  * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
   735  * terminated and mutable, regardless of the argument.
   736  * 
   737  * @param allocator a valid instance of a UcxAllocator
   738  * @param string the string to duplicate
   739  * @return a duplicate of the string
   740  * @see scstrdup()
   741  */
   742 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
   744 /**
   745  * Alias for scstrdup_a() which automatically converts the argument.
   746  * 
   747  * @param allocator a valid instance of a UcxAllocator
   748  * @param string the string to duplicate
   749  * @return a duplicate of the string
   750  * @see scstrdup()
   751  */
   752 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
   755 /**
   756  * Omits leading and trailing spaces.
   757  * 
   758  * This function returns a new sstr_t containing a trimmed version of the
   759  * specified string.
   760  * 
   761  * <b>Note:</b> the new sstr_t references the same memory, thus you
   762  * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
   763  * <code>free()</code>. It is also highly recommended to avoid assignments like
   764  * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
   765  * source string. Assignments of this type are only permitted, if the
   766  * sstr_t.ptr of the source string does not need to be freed or if another
   767  * reference to the source string exists.
   768  * 
   769  * @param string the string that shall be trimmed
   770  * @return a new sstr_t containing the trimmed string
   771  */
   772 sstr_t sstrtrim(sstr_t string);
   774 /**
   775  * Omits leading and trailing spaces.
   776  * 
   777  * This function returns a new scstr_t containing a trimmed version of the
   778  * specified string.
   779  * 
   780  * <b>Note:</b> the new scstr_t references the same memory, thus you
   781  * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
   782  * <code>free()</code>. It is also highly recommended to avoid assignments like
   783  * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
   784  * source string. Assignments of this type are only permitted, if the
   785  * scstr_t.ptr of the source string does not need to be freed or if another
   786  * reference to the source string exists.
   787  * 
   788  * @param string the string that shall be trimmed
   789  * @return a new scstr_t containing the trimmed string
   790  */
   791 scstr_t scstrtrim(scstr_t string);
   793 /**
   794  * Checks, if a string has a specific prefix.
   795  * @param string the string to check
   796  * @param prefix the prefix the string should have
   797  * @return 1, if and only if the string has the specified prefix, 0 otherwise
   798  */
   799 int scstrprefix(scstr_t string, scstr_t prefix);
   801 /**
   802  * Alias for scstrprefix() which automatically converts the arguments.
   803  * 
   804  * @param string the string to check
   805  * @param prefix the prefix the string should have
   806  * @return 1, if and only if the string has the specified prefix, 0 otherwise
   807  */
   808 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
   810 /**
   811  * Checks, if a string has a specific suffix.
   812  * @param string the string to check
   813  * @param suffix the suffix the string should have
   814  * @return 1, if and only if the string has the specified suffix, 0 otherwise
   815  */
   816 int scstrsuffix(scstr_t string, scstr_t suffix);
   818 /**
   819  * Alias for scstrsuffix() which automatically converts the arguments.
   820  *
   821  * @param string the string to check
   822  * @param suffix the suffix the string should have
   823  * @return 1, if and only if the string has the specified suffix, 0 otherwise
   824  */
   825 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
   827 /**
   828  * Returns a lower case version of a string.
   829  * 
   830  * This function creates a duplicate of the input string, first. See the
   831  * documentation of scstrdup() for the implications.
   832  * 
   833  * @param string the input string
   834  * @return the resulting lower case string
   835  * @see scstrdup()
   836  */
   837 sstr_t scstrlower(scstr_t string);
   839 /**
   840  * Alias for scstrlower() which automatically converts the argument.
   841  * 
   842  * @param string the input string
   843  * @return the resulting lower case string
   844  */
   845 #define sstrlower(string) scstrlower(SCSTR(string))
   847 /**
   848  * Returns a lower case version of a string.
   849  * 
   850  * This function creates a duplicate of the input string, first. See the
   851  * documentation of scstrdup_a() for the implications.
   852  * 
   853  * @param allocator the allocator used for duplicating the string
   854  * @param string the input string
   855  * @return the resulting lower case string
   856  * @see scstrdup_a()
   857  */
   858 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
   861 /**
   862  * Alias for scstrlower_a() which automatically converts the argument.
   863  * 
   864  * @param allocator the allocator used for duplicating the string
   865  * @param string the input string
   866  * @return the resulting lower case string
   867  */
   868 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
   870 /**
   871  * Returns a upper case version of a string.
   872  * 
   873  * This function creates a duplicate of the input string, first. See the
   874  * documentation of scstrdup() for the implications.
   875  * 
   876  * @param string the input string
   877  * @return the resulting upper case string
   878  * @see scstrdup()
   879  */
   880 sstr_t scstrupper(scstr_t string);
   882 /**
   883  * Alias for scstrupper() which automatically converts the argument.
   884  * 
   885  * @param string the input string
   886  * @return the resulting upper case string
   887  */
   888 #define sstrupper(string) scstrupper(SCSTR(string))
   890 /**
   891  * Returns a upper case version of a string.
   892  * 
   893  * This function creates a duplicate of the input string, first. See the
   894  * documentation of scstrdup_a() for the implications.
   895  * 
   896  * @param allocator the allocator used for duplicating the string
   897  * @param string the input string
   898  * @return the resulting upper case string
   899  * @see scstrdup_a()
   900  */
   901 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
   903 /**
   904  * Alias for scstrupper_a() which automatically converts the argument.
   905  * 
   906  * @param allocator the allocator used for duplicating the string
   907  * @param string the input string
   908  * @return the resulting upper case string
   909  */
   910 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
   912 #ifdef	__cplusplus
   913 }
   914 #endif
   916 #endif	/* UCX_STRING_H */

mercurial