391 lines
11 KiB
C
391 lines
11 KiB
C
#include "strops.h"
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
|
|
/* Function template
|
|
char* strops_(const char* string) {
|
|
char* result = strops_copy(string);
|
|
return result;
|
|
}
|
|
*/
|
|
|
|
ull_t strops_length(const char* string) {
|
|
if (!string) {
|
|
return 0;
|
|
}
|
|
|
|
ull_t string_length = 0;
|
|
while(string[string_length] != '\0') {
|
|
string_length++;
|
|
}
|
|
|
|
return string_length;
|
|
}
|
|
|
|
char* strops_copy(const char* string) {
|
|
if (!string) {
|
|
return 0;
|
|
}
|
|
ull_t length = strops_length(string);
|
|
if (length == 0) {
|
|
return 0;
|
|
}
|
|
char* result = malloc(length);
|
|
|
|
ull_t i;
|
|
for(i = 0; i < length; i++) {
|
|
result[i] = string[i];
|
|
}
|
|
result[i] = '\0';
|
|
|
|
return result;
|
|
}
|
|
|
|
char* strops_copy_amount(const char* string, ull_t amount) {
|
|
if (!string) {
|
|
return 0;
|
|
}
|
|
if (amount > strops_length(string)) {
|
|
return 0;
|
|
}
|
|
char* result = malloc(amount);
|
|
|
|
ull_t i;
|
|
for(i = 0; i < amount; i++) {
|
|
result[i] = string[i];
|
|
}
|
|
result[i] = '\0';
|
|
|
|
return result;
|
|
}
|
|
|
|
ull_t strops_first_pos_of_char(const char* string, char char_to_search) {
|
|
ull_t i;
|
|
for (i = 0; string[i] != char_to_search; i++) {}
|
|
return i;
|
|
}
|
|
|
|
ull_t strops_first_pos_of_string(const char* string, const char* string_to_search) {
|
|
bool_t contains_string;
|
|
ull_t sts_length = strops_length(string_to_search);
|
|
ull_t l = strops_length(string) - sts_length + 1;
|
|
ull_t i, j;
|
|
for (i = 0; i < l; i++) {
|
|
contains_string = 1;
|
|
for(j = 0; j < sts_length; j++) {
|
|
if (string[i + j] != string_to_search[j]) {
|
|
contains_string = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (contains_string) {
|
|
break;
|
|
}
|
|
}
|
|
return i;
|
|
}
|
|
|
|
bool_t strops_contains_char(const char* string, char char_to_search) {
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string) + 1; i++) {
|
|
if (string[i] == char_to_search) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool_t strops_contains_string(const char* string, const char* string_to_search) {
|
|
bool_t contains_string = 0;
|
|
ull_t sts_length = strops_length(string_to_search);
|
|
ull_t l = strops_length(string) - sts_length + 1;
|
|
ull_t i, j;
|
|
for (i = 0; i < l; i++) {
|
|
contains_string = 1;
|
|
for(j = 0; j < sts_length; j++) {
|
|
if (string[i + j] != string_to_search[j]) {
|
|
contains_string = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (contains_string) {
|
|
break;
|
|
}
|
|
}
|
|
return contains_string;
|
|
}
|
|
|
|
bool_t strops_equals(const char* string1, const char* string2) {
|
|
if (strops_length(string1) != strops_length(string2)) {
|
|
return 0;
|
|
}
|
|
bool_t equal = 1;
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string1); i++) {
|
|
if (string1[i] != string2[i]) {
|
|
equal = 0;
|
|
break;
|
|
}
|
|
}
|
|
return equal;
|
|
}
|
|
|
|
bool_t strops_starts_with(const char* string1, const char* string2) {
|
|
assert(strops_length(string1) >= strops_length(string2) && "string2 cannot be longer than string1");
|
|
bool_t starts_with = 1;
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string2); i++) {
|
|
if (string1[i] != string2[i]) {
|
|
starts_with = 0;
|
|
break;
|
|
}
|
|
}
|
|
return starts_with;
|
|
}
|
|
|
|
char* strops_to_lowercase(const char* string) {
|
|
char* result = strops_copy(string);
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string); i++) {
|
|
if (result[i] >= 'A' && result[i] <= 'Z') {
|
|
result[i] += 32;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
char* strops_to_uppercase(const char* string) {
|
|
char* result = strops_copy(string);
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string); i++) {
|
|
if (result[i] >= 'a' && result[i] <= 'z') {
|
|
result[i] -= 32;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool_t strops_is_lowercase(const char* string) {
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string); i++) {
|
|
if (!(string[i] >= 'a' && string[i] <= 'z')) {
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
bool_t strops_is_uppercase(const char* string) {
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string); i++) {
|
|
if (!(string[i] >= 'A' && string[i] <= 'Z')) {
|
|
return 0;
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
char* strops_insert_at_pos_string(const char* string, const char* string_to_insert, ull_t pos) {
|
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
|
ull_t string_length = strops_length(string) + strops_length(string_to_insert) + 1;
|
|
char* result = strops_copy(string);
|
|
ull_t i, j;
|
|
/* Make space for string_to_insert */
|
|
for (i = string_length - 1; i > pos; i--) {
|
|
result[i] = result[i - strops_length(string_to_insert)];
|
|
}
|
|
/* Insert string into empty space */
|
|
for (j = 0; j < strops_length(string_to_insert); j++) {
|
|
result[pos + j] = string_to_insert[j];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
char* strops_remove_at_pos_char(const char* string, ull_t pos) {
|
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
|
char* result = strops_copy(string);
|
|
result[pos] = 0;
|
|
ull_t i;
|
|
for (i = pos; i < strops_length(string); i++) {
|
|
result[i] = result[i + 1];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void strops_remove_at_pos_char_inplace(char* string, ull_t pos) {
|
|
assert(pos <= strops_length(string) && "pos needs to be inside string");
|
|
ull_t length = strops_length(string);
|
|
string[pos] = 0;
|
|
ull_t i;
|
|
for (i = pos; i < length; i++) {
|
|
string[i] = string[i + 1];
|
|
}
|
|
}
|
|
|
|
char* strops_trim_right_whitespace(const char* string) {
|
|
char* result = strops_copy(string);
|
|
char* tmp;
|
|
while (strops_contains_char("\t\n\v\f\r ", result[strops_length(result) - 1])) {
|
|
strops_remove_at_pos_char_inplace(result, strops_length(result) - 1);
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_left_whitespace(const char* string) {
|
|
char* result = strops_copy(string);
|
|
while (strops_contains_char("\t\n\v\f\r ", result[0])) {
|
|
strops_remove_at_pos_char_inplace(result, 0);
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_both_whitespace(const char* string) {
|
|
char* result;
|
|
char* tmp;
|
|
tmp = strops_trim_right_whitespace(string);
|
|
result = strops_trim_left_whitespace(tmp);
|
|
free(tmp);
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_right_chars(const char* string, const char* chars_to_remove) {
|
|
char* result = strops_copy(string);
|
|
while (strops_contains_char(chars_to_remove, result[strops_length(result) - 1])) {
|
|
strops_remove_at_pos_char_inplace(result, strops_length(result) - 1);
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_left_chars(const char* string, const char* chars_to_remove) {
|
|
char* result = strops_copy(string);
|
|
while (strops_contains_char(chars_to_remove, result[0])) {
|
|
strops_remove_at_pos_char_inplace(result, 0);
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_both_chars(const char* string, const char* chars_to_remove) {
|
|
char* result;
|
|
char* tmp;
|
|
tmp = strops_trim_right_chars(string, chars_to_remove);
|
|
result = strops_trim_left_chars(tmp, chars_to_remove);
|
|
free(tmp);
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_right_string(const char* string, const char* string_to_remove) {
|
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
|
char* result = strops_copy(string);
|
|
ull_t offset = strops_length(result) - strops_length(string_to_remove);
|
|
char* tmp = result + offset;
|
|
|
|
while (strcmp(tmp, string_to_remove) == 0) {
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
|
strops_remove_at_pos_char_inplace(result, offset);
|
|
}
|
|
offset = strops_length(result) - strops_length(string_to_remove);
|
|
tmp = result + offset;
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_left_string(const char* string, const char* string_to_remove) {
|
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
|
char* result = strops_copy(string);
|
|
char* tmp = strops_copy(string_to_remove);
|
|
|
|
while (strcmp(tmp, string_to_remove) == 0) {
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
|
strops_remove_at_pos_char_inplace(result, 0);
|
|
}
|
|
tmp = strops_copy_amount(result, strops_length(string_to_remove));
|
|
}
|
|
free(tmp);
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_trim_both_string(const char* string, const char* string_to_remove) {
|
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
|
char* result;
|
|
char* tmp;
|
|
tmp = strops_trim_right_string(string, string_to_remove);
|
|
result = strops_trim_left_string(tmp, string_to_remove);
|
|
return result;
|
|
}
|
|
|
|
char* strops_remove_chars(const char* string, const char* chars_to_remove) {
|
|
char* result = strops_copy(string);
|
|
|
|
ull_t i, j;
|
|
for (i = 0; i < strops_length(chars_to_remove); i++) {
|
|
while (strops_contains_char(result, chars_to_remove[i])) {
|
|
strops_remove_at_pos_char_inplace(result, strops_first_pos_of_char(result, chars_to_remove[i]));
|
|
}
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
char* strops_remove_string(const char* string, const char* string_to_remove) {
|
|
assert(strops_length(string) >= strops_length(string_to_remove) && "string_to_remove cannot be bigger than string");
|
|
char* result = strops_copy(string);
|
|
|
|
while (strops_contains_string(result, string_to_remove)) {
|
|
ull_t offset = strops_first_pos_of_string(result, string_to_remove);
|
|
ull_t i;
|
|
for (i = 0; i < strops_length(string_to_remove); i++) {
|
|
strops_remove_at_pos_char_inplace(result, offset);
|
|
}
|
|
}
|
|
result = realloc(result, strops_length(result));
|
|
return result;
|
|
}
|
|
|
|
ull_t strops_word_count(const char* string) {
|
|
ull_t word_count = 0;
|
|
char* tmp = strops_copy(string);
|
|
|
|
ull_t i = 0;
|
|
ull_t beginning_of_word = 0;
|
|
while (i < strops_length(string) - 1) {
|
|
for (; i < strops_length(tmp); i++) {
|
|
if ((tmp[i] >= 'A' && tmp[i] <= 'Z') || (tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
|
beginning_of_word = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!strops_contains_char("\t\n\v\f\r ", tmp[beginning_of_word - 1])) {
|
|
continue;
|
|
}
|
|
|
|
for (i = beginning_of_word; i < strops_length(tmp); i++) {
|
|
if (!(tmp[i] >= 'A' && tmp[i] <= 'Z') && !(tmp[i] >= 'a' && tmp[i] <= 'z')) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!strops_contains_char("\t\n\v\f\r ", tmp[i + 1])) {
|
|
continue;
|
|
}
|
|
|
|
if (i - beginning_of_word < 2) {
|
|
continue;
|
|
}
|
|
|
|
word_count++;
|
|
}
|
|
free(tmp);
|
|
return word_count;
|
|
}
|
|
|