LCOV - code coverage report
Current view: top level - include/internal - cascii.h (source / functions) Coverage Total Hit
Test: c_collections 2.0.0 Lines: 100.0 % 20 20
Test Date: 2026-11-15 18:57:05 Functions: 100.0 % 1 1
Legend: Lines: hit not hit

            Line data    Source code
       1              : /*
       2              : MIT License
       3              : 
       4              : Copyright (c) 2026 - A bunch of nerds
       5              : 
       6              : Permission is hereby granted, free of charge, to any person obtaining a copy
       7              : of this software and associated documentation files (the "Software"), to deal
       8              : in the Software without restriction, including without limitation the rights
       9              : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      10              : copies of the Software, and to permit persons to whom the Software is
      11              : furnished to do so, subject to the following conditions:
      12              : 
      13              : The above copyright notice and this permission notice shall be included in all
      14              : copies or substantial portions of the Software.
      15              : 
      16              : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17              : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18              : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19              : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20              : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      21              : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      22              : SOFTWARE.
      23              : */
      24              : 
      25              : #pragma once
      26              : 
      27              : // INTERNAL ONLY. Case folding and case-insensitive comparison of ASCII text,
      28              : // independent of the locale. HTTP field names, tokens such as "chunked", URI
      29              : // schemes and host names follow the case rules of the protocol. strcasecmp()
      30              : // and tolower() follow LC_CTYPE, and under a Turkish locale tolower('I') isn't
      31              : // 'i', so "TRANSFER-ENCODING" stops matching. Every comparison of protocol text
      32              : // goes through these helpers, which fold only 'A' to 'Z'. No test in the C
      33              : // locale shows the mistake, so please use them for any new comparison.
      34              : 
      35              : #ifndef CCOL_INTERNAL_CASCII_H
      36              : #define CCOL_INTERNAL_CASCII_H
      37              : 
      38              : #include <stddef.h>
      39              : 
      40              : // Gives c in lower case when it's an ASCII upper-case letter, else c. It's the
      41              : // locale-free tolower() that every fold of protocol text uses.
      42       584998 : static inline int ccol_ascii_tolower(int c) {
      43       195457 :   return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c;
      44              : }
      45              : 
      46              : // Gives c in upper case when it's an ASCII lower-case letter, else c; the
      47              : // locale-free toupper() that cstring's case conversion uses.
      48           49 : static inline int ccol_ascii_toupper(int c) {
      49           49 :   return (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c;
      50              : }
      51              : 
      52              : // Says whether c is ASCII white space; cstring_trim() uses it so the locale
      53              : // can't change what counts as space. True for the six bytes that isspace()
      54              : // reports in the C locale: Space, '\t', '\n', '\v', '\f' and '\r'. No other
      55              : // byte counts as white space, whatever the locale says.
      56           70 : static inline int ccol_ascii_isspace(int c) {
      57           70 :   return c == ' ' || (c >= '\t' && c <= '\r');
      58              : }
      59              : 
      60              : // Compares two strings ignoring ASCII case, for protocol names and tokens;
      61              : // strcasecmp() with ASCII folding only. Gives < 0, 0 or > 0 and compares the
      62              : // folded bytes as unsigned char.
      63         2019 : static inline int ccol_ascii_casecmp(const char *a, const char *b) {
      64         2019 :   const unsigned char *p = (const unsigned char *)a;
      65         2019 :   const unsigned char *q = (const unsigned char *)b;
      66        31830 :   for (;; p++, q++) {
      67        41664 :     int x = ccol_ascii_tolower(*p);
      68        41664 :     int y = ccol_ascii_tolower(*q);
      69        41664 :     if (x != y || x == 0) return x - y;
      70              :   }
      71              : }
      72              : 
      73              : // Compares at most n bytes ignoring ASCII case, for protocol text that isn't
      74              : // NUL-terminated where it ends; strncasecmp() with ASCII folding only.
      75        20657 : static inline int ccol_ascii_ncasecmp(const char *a, const char *b, size_t n) {
      76        20657 :   const unsigned char *p = (const unsigned char *)a;
      77        20657 :   const unsigned char *q = (const unsigned char *)b;
      78       175649 :   for (; n > 0; n--, p++, q++) {
      79       160780 :     int x = ccol_ascii_tolower(*p);
      80       160780 :     int y = ccol_ascii_tolower(*q);
      81       160780 :     if (x != y || x == 0) return x - y;
      82              :   }
      83              :   return 0;
      84              : }
      85              : 
      86              : #endif  // CCOL_INTERNAL_CASCII_H
        

Generated by: LCOV version 2.0-1