|
1 | | -# string-view-c |
2 | | -String View library for C programming language |
| 1 | +# String view implementation for C language |
| 2 | + |
| 3 | +This is a simple porting of C++ `std::string_view` for the C programming language. |
| 4 | +This library is header-only and follows the [stb-style](https://github.com/nothings/stb). |
| 5 | +For more information about this style read [this](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt). |
| 6 | + |
| 7 | +> [!IMPORTANT] |
| 8 | +> This library uses **C99** features ([Inline functions](https://en.wikipedia.org/wiki/Inline_function)). |
| 9 | +
|
| 10 | +```c |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | + |
| 14 | +#define STRING_VIEW_IMPLEMENTATION |
| 15 | +#include "string_view.h" |
| 16 | + |
| 17 | +int main(int argc, char** argv) { |
| 18 | + |
| 19 | + string_view_t hello = new_string_view_from_cstr("Hello string views"); |
| 20 | + printf(STRING_VIEW_FORMAT "\n", STRING_VIEW_ARG(hello)); |
| 21 | + |
| 22 | + return 0; |
| 23 | +} |
| 24 | + |
| 25 | +// Output: Hello string views |
| 26 | + |
| 27 | +``` |
| 28 | +
|
| 29 | +## How use this library? |
| 30 | +
|
| 31 | +Simple just include it and before the include directive add `#define STRING_VIEW_IMPLEMENTATION`. |
| 32 | +No linking or stuff like that is needed. |
| 33 | +
|
| 34 | +> [!TIP] |
| 35 | +> If in your project string views are shared among multiple files |
| 36 | +> you need to create another file and put there the implmentation of the library. |
| 37 | +> See the above example: |
| 38 | +> ```c |
| 39 | +> // string_view_impl.c |
| 40 | +> |
| 41 | +> #define STRING_VIEW_IMPLEMENTATION |
| 42 | +> #include "string_view.h" |
| 43 | +> |
| 44 | +> ``` |
| 45 | +> |
| 46 | +> ```c |
| 47 | +> // my_project_file.c |
| 48 | +> |
| 49 | +> #include <stdio.h> |
| 50 | +> #include "string_view.h" |
| 51 | +> |
| 52 | +> void print_view(string_view_t sv) { |
| 53 | +> printf(STRING_VIEW_FORMAT "\n", STRING_VIEW_ARG(sv)); |
| 54 | +> } |
| 55 | +> ``` |
| 56 | +
|
| 57 | +## Documentation |
| 58 | +
|
| 59 | +The full documentation is situated inside the [string_view.h](./string_view.h) file. |
| 60 | +
|
| 61 | +## Implemented functions |
| 62 | +
|
| 63 | +| Function | Brief | |
| 64 | +|------------------------------|------------------------------------------------------------------------| |
| 65 | +| `new_string_view` | Creates a new string view from a character array and length | |
| 66 | +| `new_string_view_from_cstr` | Creates a new string view from a null-terminated character string | |
| 67 | +| `string_view_data` | Returns a pointer to the underlying character data of the string view | |
| 68 | +| `string_view_size` | Returns the length of the string view | |
| 69 | +| `string_view_at` | Returns the character at the specified index in the string view | |
| 70 | +| `string_view_front` | Returns the first character of the string view | |
| 71 | +| `string_view_back` | Returns the last character of the string view | |
| 72 | +| `string_view_is_empty` | Checks if the string view is empty | |
| 73 | +| `string_view_trim_left` | Trims whitespace characters from the beginning of a string view | |
| 74 | +| `string_view_trim_right` | Trims whitespace characters from the end of a string view | |
| 75 | +| `string_view_trim` | Trims whitespace characters from both ends of a string view | |
| 76 | +| `string_view_remove_prefix` | Removes a prefix of specified length from a string view | |
| 77 | +| `string_view_remove_suffix` | Removes a suffix of specified length from a string view | |
| 78 | +| `string_view_swap` | Swaps the contents of two string views | |
| 79 | +| `string_view_copy` | Copies a portion of a string view into a character array | |
| 80 | +| `string_view_substr` | Creates a new string view that is a substring of the given string view | |
| 81 | +| `string_view_compare` | Compares two string views lexicographically | |
| 82 | +| `string_view_equal` | Checks if two string views are equal | |
| 83 | +| `string_view_starts_with` | Checks if a string view starts with a given prefix | |
| 84 | +| `string_view_ends_with` | Checks if a string view ends with a given suffix | |
| 85 | +| `string_view_find_char` | Finds the first occurrence of a character in a string view | |
| 86 | +| `string_view_find_substring` | Finds the first occurrence of a substring within a string view | |
| 87 | +
|
| 88 | +> [!NOTE] |
| 89 | +> **string_view_contains** is dependent on the C standard version. |
| 90 | +> |
| 91 | +> In **C11** is defined as a macro that uses `_Generic` to dispatch the correct implementation. |
| 92 | +> |
| 93 | +> | Macro | Brief | |
| 94 | +> |----------------------------------|------------------------------------------------------| |
| 95 | +> | `string_view_contains` | Checks if a string view contains a specified element | |
| 96 | +> |
| 97 | +> In **C99**, two separate macros are defined for character and substring checks: |
| 98 | +> |
| 99 | +> | Macro | Brief | |
| 100 | +> |----------------------------------|-------------------------------------------------------| |
| 101 | +> | `string_view_contains_char` | Checks if a string view contains a specific character | |
| 102 | +> | `string_view_contains_substring` | Checks if a string view contains a specific substring | |
| 103 | +> |
| 104 | +
|
0 commit comments