1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-12-25 17:19:46 +05:30

C: add string_case.*

This commit is contained in:
Intel A80486DX2-66 2024-06-25 16:40:18 +03:00
parent 006b4ec9b2
commit d285fe2950
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,38 @@
/*
* string_case.c
*
* Author: Intel A80486DX2-66
* License: Unlicense
*/
#include "string_case.h"
char* ascii_lowercase_str(const char* s) { ASCII_LOWERCASE_STR_T(s); }
char* ascii_lowercase_strm(char* s) { ASCII_LOWERCASE_STRM_T(s); }
char* ascii_uppercase_str(const char* s) { ASCII_UPPERCASE_STR_T(s); }
char* ascii_uppercase_strm(char* s) { ASCII_UPPERCASE_STRM_T(s); }
#ifdef TEST
# include <stdio.h>
# include <stdlib.h>
int main(void) {
printf("Constant string:\n");
const char* const_str = "Hello, World!";
char* str1 = ascii_lowercase_str(const_str),
* str2 = ascii_uppercase_str(const_str);
printf("original string: %s\n", const_str);
printf("lower: %s\n", str1); free(str1);
printf("upper: %s\n", str2); free(str2);
printf("original string: %s\n", const_str);
printf("\nNon-constant string:\n");
char non_const_str[] = "Hello, user!";
printf("original string: %s\n", non_const_str);
printf("lower: %s\n", ascii_lowercase_strm(non_const_str));
printf("upper: %s\n", ascii_uppercase_strm(non_const_str));
printf("original string: %s\n", non_const_str);
return EXIT_SUCCESS;
}
#endif

View File

@ -0,0 +1,51 @@
/*
* string_case.h
*
* Author: Intel A80486DX2-66
* License: Unlicense
*/
#ifndef _STRING_CASE_H
#define _STRING_CASE_H
#ifdef __STDC_ALLOC_LIB__
# define __STDC_WANT_LIB_EXT2__ 1
#else
# define _POSIX_C_SOURCE 200809L
#endif
#include <stdlib.h>
#include <string.h>
#define ASCII_IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
#define ASCII_IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
#define ASCII_IS_ALPHA(c) (ASCII_IS_LOWER(c) || ASCII_IS_UPPER(c))
#define ASCII_MOD_ALPHA(c, v) (ASCII_IS_ALPHA(c) ? v : c)
#define ASCII_LOWERCASE(c) (ASCII_MOD_ALPHA((c), (c) | 0x20))
#define ASCII_UPPERCASE(c) (ASCII_MOD_ALPHA((c), ASCII_LOWERCASE(c) - 0x20))
#define ASCII_MOD_STRM(s, m) \
char* ptr = s; \
while (*ptr) { \
*ptr = m(*ptr); \
ptr++; \
} \
return s
#define ASCII_MOD_STR(s, m) \
char* s_dup = strdup((s)), * ptr = s_dup; \
while (*ptr) { \
*ptr = m(*ptr); \
ptr++; \
} \
return s_dup
#define ASCII_UPPERCASE_STRM_T(s) ASCII_MOD_STRM(s, ASCII_UPPERCASE)
#define ASCII_LOWERCASE_STRM_T(s) ASCII_MOD_STRM(s, ASCII_LOWERCASE)
#define ASCII_UPPERCASE_STR_T(s) ASCII_MOD_STR(s, ASCII_UPPERCASE)
#define ASCII_LOWERCASE_STR_T(s) ASCII_MOD_STR(s, ASCII_LOWERCASE)
char* ascii_lowercase_str(const char* s);
char* ascii_lowercase_strm(char* s);
char* ascii_uppercase_str(const char* s);
char* ascii_uppercase_strm(char* s);
#endif /* _STRING_CASE_H */