LCOV - code coverage report
Current view: top level - monetdb5/modules/atoms - str.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 26 30 86.7 %
Date: 2021-10-13 02:24:04 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #ifndef __string_H__
      10             : #define __string_H__
      11             : #include "gdk.h"
      12             : #include "mal.h"
      13             : #include "mal_exception.h"
      14             : #include <ctype.h>
      15             : 
      16             : /* The batstr module functions use a single buffer to avoid malloc/free overhead.
      17             :    Note the buffer should be always large enough to hold null strings, so less testing will be required */
      18             : #define INITIAL_STR_BUFFER_LENGTH (MAX(strlen(str_nil) + 1, 1024))
      19             : 
      20             : /* The batstr module functions use a single buffer to avoid malloc/free overhead.
      21             :    Note the buffer should be always large enough to hold null strings, so less testing will be required */
      22             : #define CHECK_STR_BUFFER_LENGTH(BUFFER, BUFFER_LEN, NEXT_LEN, OP) \
      23             :         do { \
      24             :                 if ((NEXT_LEN) > *BUFFER_LEN) { \
      25             :                         size_t newlen = (((NEXT_LEN) + 1023) & ~1023); /* align to a multiple of 1024 bytes */ \
      26             :                         str newbuf = GDKmalloc(newlen); \
      27             :                         if (!newbuf) \
      28             :                                 throw(MAL, OP, SQLSTATE(HY013) MAL_MALLOC_FAIL); \
      29             :                         GDKfree(*BUFFER); \
      30             :                         *BUFFER = newbuf; \
      31             :                         *BUFFER_LEN = newlen; \
      32             :                 } \
      33             :         } while (0)
      34             : 
      35             : #ifndef NDEBUG
      36             : static void
      37    34278569 : UTF8_assert(const char *restrict s)
      38             : {
      39             :         int c;
      40             : 
      41    34278569 :         if (s == NULL)
      42             :                 return;
      43    34278569 :         if (*s == '\200' && s[1] == '\0')
      44             :                 return;                                 /* str_nil */
      45  1351090133 :         while ((c = *s++) != '\0') {
      46  1316811564 :                 if ((c & 0x80) == 0)
      47  1316809695 :                         continue;
      48        1869 :                 if ((*s++ & 0xC0) != 0x80)
      49           0 :                         assert(0);
      50        1869 :                 if ((c & 0xE0) == 0xC0)
      51        1640 :                         continue;
      52         229 :                 if ((*s++ & 0xC0) != 0x80)
      53           0 :                         assert(0);
      54         229 :                 if ((c & 0xF0) == 0xE0)
      55         226 :                         continue;
      56           3 :                 if ((*s++ & 0xC0) != 0x80)
      57           0 :                         assert(0);
      58           3 :                 if ((c & 0xF8) == 0xF0)
      59           3 :                         continue;
      60           0 :                 assert(0);
      61             :         }
      62             : }
      63             : #else
      64             : #define UTF8_assert(s)          ((void) 0)
      65             : #endif
      66             : 
      67             : static inline int
      68    27757520 : UTF8_strlen(const char *restrict s) /* This function assumes, s is never nil */
      69             : {
      70             :         size_t pos = 0;
      71             : 
      72    27757520 :         UTF8_assert(s);
      73    27756043 :         assert(!strNil(s));
      74             : 
      75   951483258 :         while (*s) {
      76             :                 /* just count leading bytes of encoded code points; only works
      77             :                  * for correctly encoded UTF-8 */
      78   923727215 :                 pos += (*s++ & 0xC0) != 0x80;
      79             :         }
      80    27756043 :         assert(pos < INT_MAX);
      81    27756043 :         return (int) pos;
      82             : }
      83             : 
      84             : static inline int
      85           4 : str_strlen(const char *restrict s)  /* This function assumes, s is never nil */
      86             : {
      87           4 :         size_t pos = strlen(s);
      88           4 :         assert(pos < INT_MAX);
      89           4 :         return (int) pos;
      90             : }
      91             : 
      92             : mal_export bool batstr_func_has_candidates(const char *func);
      93             : 
      94             : /* For str returning functions, the result is passed as the input parameter buf. The returned str indicates
      95             :    if the function succeeded (ie malloc failure or invalid unicode character). str_wchr_at function also
      96             :    follows this pattern. */
      97             : 
      98             : /* Warning, the following functions don't test for NULL values, that's resposibility from the caller */
      99             : 
     100             : extern str str_from_wchr(str *buf, size_t *buflen, int c)
     101             : __attribute__((__visibility__("hidden")));
     102             : extern str str_wchr_at(int *res, str s, int at)
     103             : __attribute__((__visibility__("hidden")));
     104             : 
     105             : extern bit str_is_prefix(str s, str prefix)
     106             : __attribute__((__visibility__("hidden")));
     107             : extern bit str_is_suffix(str s, str suffix)
     108             : __attribute__((__visibility__("hidden")));
     109             : 
     110             : extern str str_tail(str *buf, size_t *buflen, str s, int off)
     111             : __attribute__((__visibility__("hidden")));
     112             : extern str str_Sub_String(str *buf, size_t *buflen, str s, int off, int l)
     113             : __attribute__((__visibility__("hidden")));
     114             : extern str str_substring_tail(str *buf, size_t *buflen, str s, int start)
     115             : __attribute__((__visibility__("hidden")));
     116             : extern str str_sub_string(str *buf, size_t *buflen, str s, int start, int l)
     117             : __attribute__((__visibility__("hidden")));
     118             : extern str str_suffix(str *buf, size_t *buflen, str s, int l)
     119             : __attribute__((__visibility__("hidden")));
     120             : extern str str_repeat(str *buf, size_t *buflen, str s, int c)
     121             : __attribute__((__visibility__("hidden")));
     122             : 
     123             : extern str str_case_hash_lock(bool upper)
     124             : __attribute__((__visibility__("hidden")));
     125             : extern void str_case_hash_unlock(bool upper)
     126             : __attribute__((__visibility__("hidden")));
     127             : /* Make sure the UTF8_toLowerFrom hash is locked! */
     128             : extern str str_lower(str *buf, size_t *buflen, str s)
     129             : __attribute__((__visibility__("hidden")));
     130             : /* Make sure the UTF8_toUpperFrom hash is locked! */
     131             : extern str str_upper(str *buf, size_t *buflen, str s)
     132             : __attribute__((__visibility__("hidden")));
     133             : 
     134             : extern str str_strip(str *buf, size_t *buflen, str s)
     135             : __attribute__((__visibility__("hidden")));
     136             : extern str str_ltrim(str *buf, size_t *buflen, str s)
     137             : __attribute__((__visibility__("hidden")));
     138             : extern str str_rtrim(str *buf, size_t *buflen, str s)
     139             : __attribute__((__visibility__("hidden")));
     140             : extern str str_strip2(str *buf, size_t *buflen, str s, str s2)
     141             : __attribute__((__visibility__("hidden")));
     142             : extern str str_ltrim2(str *buf, size_t *buflen, str s, str s2)
     143             : __attribute__((__visibility__("hidden")));
     144             : extern str str_rtrim2(str *buf, size_t *buflen, str s, str s2)
     145             : __attribute__((__visibility__("hidden")));
     146             : extern str str_lpad(str *buf, size_t *buflen, str s, int len)
     147             : __attribute__((__visibility__("hidden")));
     148             : extern str str_rpad(str *buf, size_t *buflen, str s, int len)
     149             : __attribute__((__visibility__("hidden")));
     150             : extern str str_lpad3(str *buf, size_t *buflen, str s, int len, str s2)
     151             : __attribute__((__visibility__("hidden")));
     152             : extern str str_rpad3(str *buf, size_t *buflen, str s, int len, str s2)
     153             : __attribute__((__visibility__("hidden")));
     154             : 
     155             : extern int str_search(str s, str s2)
     156             : __attribute__((__visibility__("hidden")));
     157             : extern int str_reverse_str_search(str s, str s2)
     158             : __attribute__((__visibility__("hidden")));
     159             : extern int str_locate2(str needle, str haystack, int start)
     160             : __attribute__((__visibility__("hidden")));
     161             : 
     162             : extern str str_splitpart(str *buf, size_t *buflen, str s, str s2, int f)
     163             : __attribute__((__visibility__("hidden")));
     164             : extern str str_insert(str *buf, size_t *buflen, str s, int strt, int l, str s2)
     165             : __attribute__((__visibility__("hidden")));
     166             : extern str str_substitute(str *buf, size_t *buflen, str s, str src, str dst, bit repeat)
     167             : __attribute__((__visibility__("hidden")));
     168             : 
     169             : #endif /* __string_H__ */

Generated by: LCOV version 1.14