ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
char.c
Go to the documentation of this file.
1//! @file ds/char.c
2//! @author Avinash Maddikonda (svasssakavi@gmail.com)
3//! @brief Implementation of the @ref ds_char_t "char" type, related constants
4//! and functions.
5//! @since 0.3.0
6//! @date 2023-07-26
7
8#include <stdbool.h>
9#include <stdint.h>
10
11#include "ds/char.h"
12#include "ds/helpers.h"
13
14/// @internal
15/// @brief If 6th bit is set, ASCII is lower case.
16static uint8_t const DS_ASCII_CASE_MASK = 0x20;
17
18/// @internal
19/// @brief Toggles the case of an ASCII alphabetic character. Converts lower
20/// case to upper case and vice versa.
21/// @param self The value to convert.
22/// @return The ASCII character in the toggled case.
23static ds_char_t
24ds_char_ascii_change_case_unchecked (ds_char_t const self)
25{
26 return (ds_char_t)((uint8_t)self ^ DS_ASCII_CASE_MASK);
27}
28
31{
32 return ds_char_is_ascii_lowercase (self)
33 ? ds_char_ascii_change_case_unchecked (self)
34 : self;
35}
36
39{
40 return ds_char_is_ascii_uppercase (self)
41 ? ds_char_ascii_change_case_unchecked (self)
42 : self;
43}
44
45bool
47{
48 return ds_char_to_ascii_lowercase (self)
50}
51
52bool
54{
55 return ds_char_is_ascii_lowercase (self)
57}
58
59bool
61{
62 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, 'A', 'Z');
63}
64
65bool
67{
68 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, 'a', 'z');
69}
70
71bool
73{
75}
76
77bool
79{
80 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, '0', '9');
81}
82
83bool
85{
86 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, '0', '7');
87}
88
89bool
91{
92 return ds_char_is_ascii_digit (self)
93 || DS_HELPERS_IN_RANGE_INCLUSIVE (self, 'a', 'f')
94 || DS_HELPERS_IN_RANGE_INCLUSIVE (self, 'A', 'F');
95}
96
97bool
99{
100 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, '!', '/')
101 || DS_HELPERS_IN_RANGE_INCLUSIVE (self, ':', '@')
102 || DS_HELPERS_IN_RANGE_INCLUSIVE (self, '[', '`')
103 || DS_HELPERS_IN_RANGE_INCLUSIVE (self, '{', '~');
104}
105
106bool
108{
109 return DS_HELPERS_IN_RANGE_INCLUSIVE (self, '!', '~');
110}
111
112bool
114{
115 return self == DS_CHAR_HT || self == DS_CHAR_LF || self == DS_CHAR_FF
116 || self == DS_CHAR_CR || self == ' ';
117}
118
119bool
121{
122 return self == DS_CHAR_DEL
124}
bool ds_char_is_ascii_alphabetic(ds_char_t const self)
Checks if self is an ASCII alphabetic character:
Definition: char.c:53
bool ds_char_is_ascii_hexdigit(ds_char_t const self)
Checks if self is an ASCII hexadecimal digit:
Definition: char.c:90
bool ds_char_is_ascii_control(ds_char_t const self)
Checks if self is an ASCII control character:
Definition: char.c:120
bool ds_char_is_ascii_alphanumeric(ds_char_t const self)
Checks if self is an ASCII alphanumeric character:
Definition: char.c:72
bool ds_char_is_ascii_whitespace(ds_char_t const self)
Checks if self is an ASCII whitespace character:
Definition: char.c:113
bool ds_char_eq_ignore_ascii_case(ds_char_t const self, ds_char_t const other)
Checks that self and other are an ASCII case-insensitive match.
Definition: char.c:46
bool ds_char_is_ascii_digit(ds_char_t const self)
Checks if self is an ASCII decimal digit:
Definition: char.c:78
bool ds_char_is_ascii_uppercase(ds_char_t const self)
Checks if self is an ASCII uppercase character:
Definition: char.c:60
ds_char_t ds_char_to_ascii_lowercase(ds_char_t const self)
Makes a copy of self in its ASCII lower case equivalent.
Definition: char.c:38
ds_char_t ds_char_to_ascii_uppercase(ds_char_t const self)
Makes a copy of self in its ASCII upper case equivalent.
Definition: char.c:30
bool ds_char_is_ascii_octdigit(ds_char_t const self)
Checks if self is an ASCII octal digit:
Definition: char.c:84
bool ds_char_is_ascii_punctuation(ds_char_t const self)
Checks if self is an ASCII punctuation character:
Definition: char.c:98
bool ds_char_is_ascii_graphic(ds_char_t const self)
Checks if self is an ASCII graphic character:
Definition: char.c:107
bool ds_char_is_ascii_lowercase(ds_char_t const self)
Checks if self is an ASCII lowercase character:
Definition: char.c:66
Declaration of the char type, related constants and functions.
char ds_char_t
A character type.
Definition: char.h:30
#define DS_CHAR_HT
The horizontal-tab character.
Definition: char.h:626
#define DS_CHAR_DEL
The delete character.
Definition: char.h:703
#define DS_CHAR_US
The unit-separator character.
Definition: char.h:692
#define DS_CHAR_CR
The carriage-return character.
Definition: char.h:670
#define DS_CHAR_LF
The line-feed character.
Definition: char.h:637
#define DS_CHAR_FF
The form-feed character.
Definition: char.h:659
#define DS_CHAR_NUL
The null character.
Definition: char.h:593
Declaration of helper constants and functions.
#define DS_HELPERS_IN_RANGE_INCLUSIVE(self, start, end)
Checks if self lies in the inclusive interval between start and end.
Definition: helpers.h:135