ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
char.c File Reference

Implementation of the char type, related constants and functions. More...

#include <stdbool.h>
#include <stdint.h>
#include "ds/char.h"
#include "ds/helpers.h"
Include dependency graph for char.c:

Go to the source code of this file.

Functions

ds_char_t ds_char_to_ascii_uppercase (ds_char_t const self)
 Makes a copy of self in its ASCII upper case equivalent. More...
 
ds_char_t ds_char_to_ascii_lowercase (ds_char_t const self)
 Makes a copy of self in its ASCII lower case equivalent. More...
 
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. More...
 
bool ds_char_is_ascii_alphabetic (ds_char_t const self)
 Checks if self is an ASCII alphabetic character: More...
 
bool ds_char_is_ascii_uppercase (ds_char_t const self)
 Checks if self is an ASCII uppercase character: More...
 
bool ds_char_is_ascii_lowercase (ds_char_t const self)
 Checks if self is an ASCII lowercase character: More...
 
bool ds_char_is_ascii_alphanumeric (ds_char_t const self)
 Checks if self is an ASCII alphanumeric character: More...
 
bool ds_char_is_ascii_digit (ds_char_t const self)
 Checks if self is an ASCII decimal digit: More...
 
bool ds_char_is_ascii_octdigit (ds_char_t const self)
 Checks if self is an ASCII octal digit: More...
 
bool ds_char_is_ascii_hexdigit (ds_char_t const self)
 Checks if self is an ASCII hexadecimal digit: More...
 
bool ds_char_is_ascii_punctuation (ds_char_t const self)
 Checks if self is an ASCII punctuation character: More...
 
bool ds_char_is_ascii_graphic (ds_char_t const self)
 Checks if self is an ASCII graphic character: More...
 
bool ds_char_is_ascii_whitespace (ds_char_t const self)
 Checks if self is an ASCII whitespace character: More...
 
bool ds_char_is_ascii_control (ds_char_t const self)
 Checks if self is an ASCII control character: More...
 

Detailed Description

Implementation of the char type, related constants and functions.

Author
Avinash Maddikonda (svass.nosp@m.saka.nosp@m.vi@gm.nosp@m.ail..nosp@m.com)
Since
0.3.0
Date
2023-07-26

Definition in file char.c.

Function Documentation

◆ ds_char_eq_ignore_ascii_case()

bool ds_char_eq_ignore_ascii_case ( ds_char_t  self,
ds_char_t  other 
)

Checks that self and other are an ASCII case-insensitive match.

Equivalent to:

ds_char_t ds_char_to_ascii_lowercase(ds_char_t self)
Makes a copy of self in its ASCII lower case equivalent.
Definition: char.c:38
Parameters
selfThe first value to compare.
otherThe second value to compare.
Returns
true if self and other are an ASCII case-insensitive match, and false otherwise.

Examples

Basic usage:

ds_char_t const upper_a = 'A';
ds_char_t const lower_a = 'a';
ds_char_t const lower_z = 'z';
assert (ds_char_eq_ignore_ascii_case (upper_a, lower_a));
assert (ds_char_eq_ignore_ascii_case (upper_a, upper_a));
assert (!ds_char_eq_ignore_ascii_case (upper_a, lower_z));
char ds_char_t
A character type.
Definition: char.h:30
bool ds_char_eq_ignore_ascii_case(ds_char_t self, ds_char_t other)
Checks that self and other are an ASCII case-insensitive match.
Definition: char.c:46

Definition at line 46 of file char.c.

Here is the call graph for this function:

◆ ds_char_is_ascii_alphabetic()

bool ds_char_is_ascii_alphabetic ( ds_char_t  self)

Checks if self is an ASCII alphabetic character:

  • U+0041 'A' ..= U+005A 'Z', or
  • U+0061 'a' ..= U+007A 'z'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII alphabetic character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (ds_char_is_ascii_alphabetic (uppercase_a));
assert (ds_char_is_ascii_alphabetic (uppercase_g));
assert (!ds_char_is_ascii_alphabetic (zero));
assert (!ds_char_is_ascii_alphabetic (percent));
assert (!ds_char_is_ascii_alphabetic (space));
bool ds_char_is_ascii_alphabetic(ds_char_t self)
Checks if self is an ASCII alphabetic character:
Definition: char.c:53
#define DS_CHAR_LF
The line-feed character.
Definition: char.h:637
#define DS_CHAR_ESC
The escape character.
Definition: char.h:681

Definition at line 53 of file char.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds_char_is_ascii_alphanumeric()

bool ds_char_is_ascii_alphanumeric ( ds_char_t  self)

Checks if self is an ASCII alphanumeric character:

  • U+0041 'A' ..= U+005A 'Z', or
  • U+0061 'a' ..= U+007A 'z', or
  • U+0030 '0' ..= U+0039 '9'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII alphanumeric character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (ds_char_is_ascii_alphanumeric (uppercase_a));
assert (ds_char_is_ascii_alphanumeric (uppercase_g));
assert (!ds_char_is_ascii_alphanumeric (percent));
bool ds_char_is_ascii_alphanumeric(ds_char_t self)
Checks if self is an ASCII alphanumeric character:
Definition: char.c:72

Definition at line 72 of file char.c.

Here is the call graph for this function:

◆ ds_char_is_ascii_control()

bool ds_char_is_ascii_control ( ds_char_t  self)

Checks if self is an ASCII control character:

  • U+0000 NUL ..= U+001F UNIT SEPARATOR, or U+007F DELETE.

Note that most ASCII whitespace characters are control characters, but SPACE is not.

Parameters
selfThe value to check.
Returns
true if self is an ASCII control character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (!ds_char_is_ascii_control (uppercase_a));
assert (!ds_char_is_ascii_control (uppercase_g));
assert (!ds_char_is_ascii_control (zero));
assert (!ds_char_is_ascii_control (percent));
assert (!ds_char_is_ascii_control (space));
assert (ds_char_is_ascii_control (esc));
bool ds_char_is_ascii_control(ds_char_t self)
Checks if self is an ASCII control character:
Definition: char.c:120

Definition at line 120 of file char.c.

◆ ds_char_is_ascii_digit()

bool ds_char_is_ascii_digit ( ds_char_t  self)

Checks if self is an ASCII decimal digit:

  • U+0030 '0' ..= U+0039 '9'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII decimal digit, and false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (!ds_char_is_ascii_digit (uppercase_a));
assert (!ds_char_is_ascii_digit (uppercase_g));
assert (!ds_char_is_ascii_digit (a));
assert (!ds_char_is_ascii_digit (g));
assert (ds_char_is_ascii_digit (zero));
assert (!ds_char_is_ascii_digit (percent));
assert (!ds_char_is_ascii_digit (space));
assert (!ds_char_is_ascii_digit (lf));
assert (!ds_char_is_ascii_digit (esc));
bool ds_char_is_ascii_digit(ds_char_t self)
Checks if self is an ASCII decimal digit:
Definition: char.c:78

Definition at line 78 of file char.c.

Here is the caller graph for this function:

◆ ds_char_is_ascii_graphic()

bool ds_char_is_ascii_graphic ( ds_char_t  self)

Checks if self is an ASCII graphic character:

  • U+0021 '!' ..= U+007E '~'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII graphic character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (ds_char_is_ascii_graphic (uppercase_a));
assert (ds_char_is_ascii_graphic (uppercase_g));
assert (ds_char_is_ascii_graphic (zero));
assert (ds_char_is_ascii_graphic (percent));
assert (!ds_char_is_ascii_graphic (space));
assert (!ds_char_is_ascii_graphic (lf));
assert (!ds_char_is_ascii_graphic (esc));
bool ds_char_is_ascii_graphic(ds_char_t self)
Checks if self is an ASCII graphic character:
Definition: char.c:107

Definition at line 107 of file char.c.

◆ ds_char_is_ascii_hexdigit()

bool ds_char_is_ascii_hexdigit ( ds_char_t  self)

Checks if self is an ASCII hexadecimal digit:

  • U+0030 '0' ..= U+0039 '9', or
  • U+0041 'A' ..= U+0046 'F', or
  • U+0061 'a' ..= U+0066 'f'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII hexadecimal digit, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (ds_char_is_ascii_hexdigit (uppercase_a));
assert (!ds_char_is_ascii_hexdigit (uppercase_g));
assert (ds_char_is_ascii_hexdigit (zero));
assert (!ds_char_is_ascii_hexdigit (percent));
assert (!ds_char_is_ascii_hexdigit (space));
assert (!ds_char_is_ascii_hexdigit (esc));
bool ds_char_is_ascii_hexdigit(ds_char_t self)
Checks if self is an ASCII hexadecimal digit:
Definition: char.c:90

Definition at line 90 of file char.c.

Here is the call graph for this function:

◆ ds_char_is_ascii_lowercase()

bool ds_char_is_ascii_lowercase ( ds_char_t  self)

Checks if self is an ASCII lowercase character:

  • U+0061 'a' ..= U+007A 'z'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII lowercase character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (!ds_char_is_ascii_lowercase (uppercase_a));
assert (!ds_char_is_ascii_lowercase (uppercase_g));
assert (!ds_char_is_ascii_lowercase (zero));
assert (!ds_char_is_ascii_lowercase (percent));
assert (!ds_char_is_ascii_lowercase (space));
bool ds_char_is_ascii_lowercase(ds_char_t self)
Checks if self is an ASCII lowercase character:
Definition: char.c:66

Definition at line 66 of file char.c.

Here is the caller graph for this function:

◆ ds_char_is_ascii_octdigit()

bool ds_char_is_ascii_octdigit ( ds_char_t  self)

Checks if self is an ASCII octal digit:

  • U+0030 '0' ..= U+0037 '7'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII octal digit, and false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const a = 'a';
ds_char_t const zero = '0';
ds_char_t const seven = '7';
ds_char_t const nine = '9';
ds_char_t const percent = '%';
ds_char_t const lf = DS_CHAR_LF;
assert (!ds_char_is_ascii_octdigit (uppercase_a));
assert (ds_char_is_ascii_octdigit (zero));
assert (ds_char_is_ascii_octdigit (seven));
assert (!ds_char_is_ascii_octdigit (nine));
assert (!ds_char_is_ascii_octdigit (percent));
bool ds_char_is_ascii_octdigit(ds_char_t self)
Checks if self is an ASCII octal digit:
Definition: char.c:84

Definition at line 84 of file char.c.

◆ ds_char_is_ascii_punctuation()

bool ds_char_is_ascii_punctuation ( ds_char_t  self)

Checks if self is an ASCII punctuation character:

  • U+0021 ..= U+002F ! " # $ % & ' ( ) * + , - . /, or
  • U+003A ..= U+0040 : ; < = > ? @, or
  • U+005B ..= U+0060 [ \ ] ^ _ `, or
  • U+007B ..= U+007E { | } ~
Parameters
selfThe value to check.
Returns
true if self is an ASCII punctuation character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (!ds_char_is_ascii_punctuation (uppercase_a));
assert (!ds_char_is_ascii_punctuation (uppercase_g));
assert (ds_char_is_ascii_punctuation (percent));
assert (!ds_char_is_ascii_punctuation (space));
bool ds_char_is_ascii_punctuation(ds_char_t self)
Checks if self is an ASCII punctuation character:
Definition: char.c:98

Definition at line 98 of file char.c.

◆ ds_char_is_ascii_uppercase()

bool ds_char_is_ascii_uppercase ( ds_char_t  self)

Checks if self is an ASCII uppercase character:

  • U+0041 'A' ..= U+005A 'Z'.
Parameters
selfThe value to check.
Returns
true if self is an ASCII uppercase character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (ds_char_is_ascii_uppercase (uppercase_a));
assert (ds_char_is_ascii_uppercase (uppercase_g));
assert (!ds_char_is_ascii_uppercase (zero));
assert (!ds_char_is_ascii_uppercase (percent));
assert (!ds_char_is_ascii_uppercase (space));
bool ds_char_is_ascii_uppercase(ds_char_t self)
Checks if self is an ASCII uppercase character:
Definition: char.c:60

Definition at line 60 of file char.c.

Here is the caller graph for this function:

◆ ds_char_is_ascii_whitespace()

bool ds_char_is_ascii_whitespace ( ds_char_t  self)

Checks if self is an ASCII whitespace character:

  • U+0020 SPACE,
  • U+0009 HORIZONTAL TAB,
  • U+000A LINE FEED,
  • U+000C FORM FEED, or
  • U+000D CARRIAGE RETURN.

ds uses the WhatWG Infra Standard's definition of ASCII whitespace. There are several other definitions in wide use. For instance, the POSIX locale includes U+000B VERTICAL TAB as well as all the above characters, but—from the very same specification—the default rule for "field splitting" in the Bourne shell considers only SPACE, HORIZONTAL TAB, and LINE FEED as whitespace.

If you are writing a program that will process an existing file format, check what that format's definition of whitespace is before using this function.

Parameters
selfThe value to check.
Returns
true if self is an ASCII whitespace character, false otherwise.

Examples

Basic usage:

ds_char_t const uppercase_a = 'A';
ds_char_t const uppercase_g = 'G';
ds_char_t const a = 'a';
ds_char_t const g = 'g';
ds_char_t const zero = '0';
ds_char_t const percent = '%';
ds_char_t const space = ' ';
ds_char_t const lf = DS_CHAR_LF;
ds_char_t const esc = DS_CHAR_ESC;
assert (!ds_char_is_ascii_whitespace (uppercase_a));
assert (!ds_char_is_ascii_whitespace (uppercase_g));
assert (!ds_char_is_ascii_whitespace (zero));
assert (!ds_char_is_ascii_whitespace (percent));
assert (ds_char_is_ascii_whitespace (space));
bool ds_char_is_ascii_whitespace(ds_char_t self)
Checks if self is an ASCII whitespace character:
Definition: char.c:113

Definition at line 113 of file char.c.

◆ ds_char_to_ascii_lowercase()

ds_char_t ds_char_to_ascii_lowercase ( ds_char_t  self)

Makes a copy of self in its ASCII lower case equivalent.

ASCII letters 'A' to 'Z' are mapped to 'a' to 'z', but non-ASCII letters are unchanged.

Parameters
selfThe value to convert.
Returns
The ASCII lower case equivalent of self.

Examples

Basic usage:

ds_char_t const ascii = 'A';
ds_char_t const non_ascii = '❤';
assert ('a' == ds_char_to_ascii_lowercase (ascii));
assert ('❤' == ds_char_to_ascii_lowercase (non_ascii));

Definition at line 38 of file char.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ds_char_to_ascii_uppercase()

ds_char_t ds_char_to_ascii_uppercase ( ds_char_t  self)

Makes a copy of self in its ASCII upper case equivalent.

ASCII letters 'a' to 'z' are mapped to 'A' to 'Z', but non-ASCII letters are unchanged.

Parameters
selfThe value to convert.
Returns
The ASCII upper case equivalent of self.

Examples

Basic usage:

ds_char_t const ascii = 'a';
ds_char_t const non_ascii = '❤';
assert ('A' == ds_char_to_ascii_uppercase (ascii));
assert ('❤' == ds_char_to_ascii_uppercase (non_ascii));
ds_char_t ds_char_to_ascii_uppercase(ds_char_t self)
Makes a copy of self in its ASCII upper case equivalent.
Definition: char.c:30

Definition at line 30 of file char.c.

Here is the call graph for this function: