1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-13 23:43:13 +01:00
libcproject/lib/character.c
Théo LUDWIG 72645da4b2
perf: mutate strings instead of copy when possible
BREAKING CHANGE: Most of string functions mutates the string instead of copying now.
This allows better performance when copy is not needed.
It also allows more granual control.
If copy is wanted, simply use `string_copy` before calling the function.

Impacted functions are:
`string_to_uppercase`, `string_to_lowercase`, `string_replace`,
`string_trim_start`, `string_trim_end`, `string_trim`,
`string_capitalize`, `string_reverse`
2023-08-06 23:17:07 +02:00

43 lines
1.2 KiB
C

#include "character.h"
void character_append(string_t string, const char character) {
size_t length = string_get_length(string);
character_append_at(string, character, length);
}
void character_append_at(string_t string, const char character, const size_t index) {
size_t length = string_get_length(string);
for (size_t index_string = length; index_string > index; index_string--) {
string[index_string] = string[index_string - 1];
}
string[index] = character;
string[length + 1] = '\0';
}
char character_to_upper(const char character) {
if (character >= 'a' && character <= 'z') {
return character + ('A' - 'a');
}
return character;
}
char character_to_lower(const char character) {
if (character >= 'A' && character <= 'Z') {
return character - ('A' - 'a');
}
return character;
}
bool character_get_is_digit(const char character) {
return character >= '0' && character <= '9';
}
unsigned char character_get_alphabet_position(const char character) {
const char letter = character_to_lower(character);
unsigned char position = 0;
if (letter >= 'a' && letter <= 'z') {
position = (letter - 'a') + 1;
}
return position;
}