Last active
January 17, 2022 20:49
-
-
Save opsJson/e63ec8ea680b264f73bcdad9846c4ae9 to your computer and use it in GitHub Desktop.
Minimal set of string functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define str_assert(condition) \ | |
{ \ | |
if (!(condition)) { \ | |
fprintf(stderr, "Expression: '%s' failed at %s:%i\n", \ | |
#condition, file, line); \ | |
return 0; \ | |
} \ | |
} | |
int indexOf(char *source, char *key, int pos, char *file, int line) { | |
int i, j; | |
size_t length; | |
str_assert(source != NULL); | |
str_assert(key != NULL); | |
str_assert(pos > -1); | |
length = strlen(source); | |
for (i=0,j=0; i<length; i++) { | |
(source[i] == key[j]) ? (j++) : (j=0); | |
if (j == strlen(key)) { | |
j = 0; | |
if (pos == 0) return i - strlen(key) + 1; | |
pos--; | |
} | |
} | |
return -1; | |
} | |
#define indexOf(source, key, pos) indexOf(source, key, pos, __FILE__, __LINE__) | |
char *substring(char *source, int start, int end, char *file, int line) { | |
str_assert(source != NULL); | |
str_assert(start > -1); | |
str_assert(end > -1); | |
str_assert(start < end); | |
strcpy(source, source + start); | |
source[end - start] = 0; | |
return source; | |
} | |
#define substring(source, start, end) substring(source, start, end, __FILE__, __LINE__) | |
char *replace(char *source, char *older, char *newer, char *file, int line) { | |
int older_index; | |
str_assert(source != NULL); | |
str_assert(older != NULL); | |
str_assert(newer != NULL); | |
older_index = indexOf(source, older, 0); | |
str_assert(older_index != -1); | |
memmove(source + older_index + strlen(newer), source + older_index + strlen(older), | |
strlen(source + older_index) - strlen(older)); | |
memcpy(source + older_index, newer, strlen(newer)); | |
return source; | |
} | |
#define replace(source, older, newer) replace(source, older, newer, __FILE__, __LINE__) | |
char *getNumbers(char *source, char *file, int line) { | |
int i, j; | |
size_t length; | |
str_assert(source != NULL); | |
length = strlen(source); | |
for (i=0,j=0; i<length; i++) { | |
if (source[i] > 47 && source[i] < 58) source[j++] = source[i]; | |
} | |
source[j] = 0; | |
return source; | |
} | |
#define getNumbers(source) getNumbers(source, __FILE__, __LINE__) | |
char *toUpper(char *source, char *file, int line) { | |
int i; | |
size_t length; | |
str_assert(source != NULL); | |
length = strlen(source); | |
for (i=0; i<length; i++) { | |
if (source[i] > 96 && source[i] < 123) source[i] = toupper(source[i]); | |
} | |
source[i] = 0; | |
return source; | |
} | |
#define toUpper(source) toUpper(source, __FILE__, __LINE__) | |
char *toLower(char *source, char *file, int line) { | |
int i; | |
size_t length; | |
str_assert(source != NULL); | |
length = strlen(source); | |
for (i=0; i<length; i++) { | |
if (source[i] > 64 && source[i] < 90) source[i] = tolower(source[i]); | |
} | |
source[i] = 0; | |
return source; | |
} | |
#define toLower(source) toLower(source, __FILE__, __LINE__) | |
char *trim(char *source, char *file, int line) { | |
int i, j, whitespace; | |
size_t length; | |
str_assert(source != NULL); | |
length = strlen(source); | |
for (i=0,j=0,whitespace=0; i<length; i++) { | |
(source[i] == ' ') ? (whitespace = 1) : (whitespace = 0); | |
if (!whitespace) source[j++] = source[i]; | |
} | |
source[j] = 0; | |
return source; | |
} | |
#define trim(source) trim(source, __FILE__, __LINE__) | |
/*/////////////////////////////////// | |
Testing: | |
///////////////////////////////////*/ | |
int main() { | |
char str[100] = "FOO bar 123"; | |
printf("substring\t%s\n", substring(str, 0, 9)); | |
printf("toLower\t\t%s\n", toLower(str)); | |
printf("toUpper\t\t%s\n", toUpper(str)); | |
printf("trim\t\t%s\n", trim(str)); | |
printf("getNumbers\t%s\n", getNumbers(str)); | |
printf("replace\t\t%s\n", replace(str, "1", "Hello, world!")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment