Skip to content

Commit

Permalink
Add more specialized search functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dr8co committed Apr 16, 2024
1 parent 4551940 commit 104c87c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 5 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mkdir build
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -G Ninja

# Build the library.
cmake --build build -j 4
cmake --build build --config Release -j 4
```

Replace `Ninja` with `"Unix Makefiles"` or another generator if Ninja is not available.
Expand All @@ -70,7 +70,8 @@ ar rcs liblite-string.a lite_string.o
To use the library, include the header file in your source code:

```c
#include "lite_string.h"
#include <lite_string.h>
...
```

Compile the source code and link it with the library:
Expand Down Expand Up @@ -103,7 +104,8 @@ with an integer value greater than 0 before including the header file.

```c
#define LITE_STRING_NO_RESTRICT 1
#include "lite_string.h"
#include <lite_string.h>
...
```
### Types and Constants
Expand Down Expand Up @@ -368,6 +370,18 @@ size_t string_find_first_of(const lite_string *const restrict s, const char c);
size_t string_find_first_not_of(const lite_string *const restrict s, const char c);
// Finds the first occurrence of a character that does not match the specified character in a string.
size_t string_find_first_of_chars(const lite_string *restrict s, const char *restrict cstr);
// Finds the first occurrence of any character in a C-string in a string.
size_t string_find_first_not_of_chars(const lite_string *restrict s, const char *restrict cstr);
// Finds the first occurrence of a character that does not match any character in a C-string in a string.
size_t string_find_last_of_chars(const lite_string *restrict s, const char *restrict cstr);
// Finds the last occurrence of any character in a C-string in a string.
size_t string_find_last_not_of_chars(const lite_string *restrict s, const char *restrict cstr);
// Finds the last occurrence of a character that does not match any character in a C-string in a string.
```

### Operations
Expand Down Expand Up @@ -423,14 +437,14 @@ while those that return pointers return `nullptr`.
Basic usage of the library:
```c
#include "lite_string.h"
#include <lite_string.h>
#include <stdio.h>
int main() {
lite_string *s = string_new();
string_append_cstr(s, "Hello, ");
string_append_cstr(s, "world!");
printf("%s\n", string_cstr(s););
printf("%s\n", string_cstr(s));
string_free(s);
return 0;
}
Expand Down
109 changes: 109 additions & 0 deletions lite_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,115 @@ size_t string_find_first_not_of(const lite_string *const restrict s, const char
return lite_string_npos;
}

/**
* @brief Finds the first occurrence of any character from a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be found.
* @return The index of the first occurrence of any character from the C-string in the string,
* or \p lite_string_npos if no character was found.
*/
size_t string_find_first_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
const size_t len = strlen(cstr);
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};

// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char)cstr[i]] = true;

// Find the first occurrence of any character from the C-string in the string
for (size_t i = 0; i < s->size; ++i) {
if (lookup[(unsigned char)s->data[i]])
return i;
}
}
return lite_string_npos;
}

/**
* @brief Finds the first occurrence of any character not present in a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be compared against.
* @return The index of the first occurrence of any character not present in the C-string in the string,
* or \p lite_string_npos if all characters match or the string is invalid.
*/
size_t string_find_first_not_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
const size_t len = strlen(cstr);
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};

// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char)cstr[i]] = true;

// Find the first occurrence of any character not present in the C-string in the string
for (size_t i = 0; i < s->size; ++i) {
if (!lookup[(unsigned char)s->data[i]])
return i;
}
}
return lite_string_npos;
}

/**
* @brief Finds the last occurrence of any character from a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be found.
* @return The index of the last occurrence of any character from the C-string in the string,
* or \p lite_string_npos if no character was found.
*/
size_t string_find_last_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
const size_t len = strlen(cstr);
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};

// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char)cstr[i]] = true;

// Find the last occurrence of any character from the C-string in the string
for (size_t i = s->size; i > 0; --i) {
if (lookup[(unsigned char)s->data[i - 1]])
return i - 1;
}
}
return lite_string_npos;
}

/**
* @brief Finds the last occurrence of any character not present in a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be compared against.
* @return The index of the last occurrence of any character not present in the C-string in the string,
* or \p lite_string_npos if all characters match or the string is invalid.
*/
size_t string_find_last_not_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
const size_t len = strlen(cstr);
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};

// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char)cstr[i]] = true;

// Find the last occurrence of any character not present in the C-string in the string
for (size_t i = s->size; i > 0; --i) {
if (!lookup[(unsigned char)s->data[i - 1]])
return i - 1;
}
}
return lite_string_npos;
}


/**
* @brief Checks if a string contains a specified character.
* @param s A pointer to the string.
Expand Down
8 changes: 8 additions & 0 deletions lite_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ size_t string_find_first_of(const lite_string *restrict s, char c);

size_t string_find_first_not_of(const lite_string *restrict s, char c);

size_t string_find_first_of_chars(const lite_string *restrict s, const char *restrict cstr);

size_t string_find_first_not_of_chars(const lite_string *restrict s, const char *restrict cstr);

size_t string_find_last_of_chars(const lite_string *restrict s, const char *restrict cstr);

size_t string_find_last_not_of_chars(const lite_string *restrict s, const char *restrict cstr);

size_t string_find_from(const lite_string *restrict s, const lite_string *restrict sub, size_t start);

size_t string_find(const lite_string *restrict s, const lite_string *restrict sub);
Expand Down

0 comments on commit 104c87c

Please sign in to comment.