LCOV - code coverage report
Current view: top level - src/libs/utility - text.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 16 16 100.0 %
Date: 2019-09-12 12:28:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include <ctype.h>
       2             : #include <stdlib.h>
       3             : #include <string.h>
       4             : 
       5             : #include <kdbassert.h>
       6             : #include <kdbhelper.h>
       7             : #include <kdblogger.h>
       8             : 
       9             : /**
      10             :  * @internal
      11             :  *
      12             :  * @brief This function returns a pointer to the first non-whitespace
      13             :  *        character in a given string.
      14             :  *
      15             :  * @pre The parameter `text` must not be `NULL`.
      16             :  *
      17             :  * @param text The string for which we want to determine the first
      18             :  *             non-whitespace character
      19             :  *
      20             :  * @return A pointer to the first non-whitespace character in `text`
      21             :  */
      22         247 : char * elektraLskip (char const * const text)
      23             : {
      24         247 :         ELEKTRA_ASSERT (text != NULL, "The Parameter `text` contains `NULL` instead of a valid string.");
      25             : 
      26             :         char const * start = text;
      27         327 :         while (isspace ((unsigned char) *start))
      28             :         {
      29          80 :                 start++;
      30             :         }
      31         247 :         return (char *) start;
      32             : }
      33             : 
      34             : /**
      35             :  * @internal
      36             :  *
      37             :  * @brief This function removes trailing whitespace from the given string.
      38             :  *
      39             :  * The address stored in the parameter `end` of this function must either
      40             :  * point to the last character of the string (the one before `'\0'`) or `NULL`.
      41             :  *
      42             :  * - If `end` is `NULL`, then the function determines the last character by
      43             :  *   calculating the length of the string.
      44             :  *
      45             :  * - If `end` is specified manually, then the function will cut of the given
      46             :  *   string right before the last consecutive whitespace character before
      47             :  *   the character stored in `*end`. After the call to this function
      48             :  *   the variable `*end` stores the address of the last character of the
      49             :  *   stripped string or `'\0'` if the stripped string is empty.
      50             :  *
      51             :  * @pre The parameter `start` must not be `NULL`.
      52             :  * @pre Since this function modifies the string in place, the memory location
      53             :  *      containing the given string must be writeable.
      54             :  *
      55             :  * @param start A pointer to the start of the string from which trailing
      56             :  *              whitespace should be removed
      57             :  * @param end A pointer to a memory location pointing to the last character
      58             :  *              (the one before `'\0'`) of the string this function should
      59             :  *              modify or `NULL`
      60             :  *
      61             :  * @return A pointer to the start of the modified string. This address is
      62             :  *         the same as `start`.
      63             :  */
      64         263 : char * elektraRstrip (char * const start, char ** end)
      65             : {
      66         263 :         ELEKTRA_ASSERT (start != NULL, "The Parameter `start` contains `NULL` instead of a valid string.");
      67             : 
      68         263 :         char * last = (end == NULL || *end == NULL) ? start + strlen (start) - 1 : *end;
      69             : 
      70         431 :         while (start <= last && isspace ((unsigned char) *last))
      71             :         {
      72         168 :                 last--;
      73             :         }
      74         263 :         *(last + 1) = '\0';
      75         263 :         if (end != NULL)
      76             :         {
      77          66 :                 *end = (last < start) ? start : last;
      78             :         }
      79             : 
      80         263 :         return start;
      81             : }
      82             : 
      83             : /**
      84             :  * @internal
      85             :  *
      86             :  * @brief This function removes leading and trailing whitespace from a given string.
      87             :  * *
      88             :  * @pre The parameter `text` must not be `NULL`.
      89             :  * @pre Since this function modifies the string in place, the memory location
      90             :  *      containing the given string must be writeable.
      91             :  *
      92             :  * @param start A pointer to the start of the string from which trailing
      93             :  *              whitespace should be removed
      94             :  *
      95             :  * @return A pointer to the start of the stripped string.
      96             :  */
      97         124 : char * elektraStrip (char * text)
      98             : {
      99         124 :         return elektraRstrip (elektraLskip (text), NULL);
     100             : }

Generated by: LCOV version 1.13