From fb4017ecb1caccd57668c044402836e715b3029c Mon Sep 17 00:00:00 2001 From: Horacio Daniel Ros Date: Sun, 15 Nov 2026 18:45:26 -0300 Subject: [PATCH] string view ahora soporta caracteres anchos y chop by type con una funcion por parametro --- c/stringview/main.c | 5 +- c/stringview/string | Bin 55024 -> 55912 bytes c/stringview/stringv.c | 66 +++++++++++---- c/stringview/stringv.h | 184 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+), 16 deletions(-) diff --git a/c/stringview/main.c b/c/stringview/main.c index da750a2..8fb6132 100644 --- a/c/stringview/main.c +++ b/c/stringview/main.c @@ -2,7 +2,7 @@ #include int main(void) { const char *data = " xHola mundo con string view- "; - StringView s2 = sv_from_cstr("😊"); + StringView s2 = sv_from_cstr("😊 hdr 😊"); StringView hola = sv_from_cstr(data); sv_ltrim(&hola); @@ -16,7 +16,8 @@ int main(void) { sv_trim(&trim); printf("El string view contiene: %s\n", s2.text); - printf("El length es: %zu\n", s2.length); + printf("El length es: %zu y la cantidad de caracteres es: %zu\n", s2.length, + sv_chars_count(&s2)); printf("hola tiene:%.*s\n", (int)hola.length, hola.text); printf("view tiene:%.*s\n", (int)view.length, view.text); diff --git a/c/stringview/string b/c/stringview/string index 9695b672cd5b701ebbabfe82854a4ea8b72854e6..5b48718dbe7f4792f47e603f127a39d8c1fcb927 100755 GIT binary patch delta 12216 zcmbU{d0bW1^XHuV&gH>l-&c9;xS}YyBZyBQ;x4Xfh=4*axu9954^c}}Q@Z9Bu9TWf zY0t{6+{z`j#5KNst+a0z=ALGJP2)E+_krlI-#@?mfthpW%*>hX%)MOh?Hx+>Ze?k_ zsIDw)@`FphE2AslRrBU8T+C9Rnx)p0@AImR)~EGY3leeOEJmk3#@YZ8`wz^eA}*pb z!lB|RT<*|OuALT3e8XzLT-x&b$i?zT=X-++PHs=nzr4j>q-=H*K1O?(R}{BZS?A8G zV$)pqpxo7S0IUlwRn~>fI{!c&bstiXT5cl?;2OkNHJr?~+~zvJC!iWDu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_#crdV~+6Zi^%d}$5+6pZPFRjv!!N10I&g4fiNwat%7Sn8A1L&4>_$@2q?@j&{ zrmmC&pn=0rz!l7F*m0;e8UDX$Ra6n;FZw=~O3ViAB+aIvQuAhcKwGRt+VU{E+RCK> zXq)?JQQKNfX2M7ec4GVxph=rBaP4$7BUuAKY_dJqVA3HOOQPdhPoSMlu0T7tLqAgD ze1N8!Fc)c4(UkPTxTnhxn6j>ogMen_V-UI>!vfB1ngi7OURR)5BM=kW9kC;H55|(| zv#b@++~(mx2c}{WY=du)1~zODdN#%aM>`_W3p3JG0Q_j~Q;xW4Iug+p(h@t5WeAiM zt%*P2U4nET2oa)r%mPHkb4|fhnzy)_0E0o^NkB)ggU&7s7@}zOcqmvKJl7+K!)zD- z5%Ij6Pr-eA_)W~)UOos5XdmAJhDiLn0oVZ^i3#|O$Dqvzc@j3FL;MilQp*=&86Dw` z(DlpQ7a{R)Zb9`cya&eSJAMy*AV9pu@n(jEk^bR zwno@ufW0jO{D8e9&f`(OYBu{O3Fx-?FKw9$2;N(c-hP9fwY zTN}YO-k}+9k^bvj6&qUi)O!^?QVUy&pp7mE!{~~CEXS&PImZI@wY6ADuiwG&PV)le z79=7q90+idwsE@ zo1zNL-?YoRiMvc*RN{ zkH@a!d$HrM<`1ELY5VvLPyjFwh5Ez4f0*>C!^B6viYRKi7!OY9*#l^Y)9FC1Kj5=u zfH#DfZVX$5{)bngX2gZ&KqKd20-}D!oVFc=l7#ILSDAPM`+t%zrmLNXs7kKHJt%-zE4iO|B|KOOJfog` zjX|xz*Nih8zN)qwTV~MeN@MleZ{OG9D*ps&0)E4Z_p;O{PiG9jl}E3h{f_@K$Q@qY z&`%xpAO0uIz2F39ap~?vHFpZec~~V+uuCy&+VH}OcC~&Q~8RR?}ey p>N75Oc4_^YWAQHRy_o16bHp+ITzCE|$>q1t9ka #include -/* - * @brief Create a string view from a const char* and set their length - * - * @details This string view provides a view of a string. It does not allocate - * memory, therefore, it lives only while the source is alive. The - * length is a bytes number of the string. It does not represents the number of - * characters. - * - * @param str pointer from the original string. It can be NULL. - * - * @return a StringView with the view of original string with corresponding - * length. - */ - StringView sv_from_cstr(const char *str) { if (str == NULL) { return SV_NULL; @@ -26,6 +12,38 @@ StringView sv_from_cstr(const char *str) { return (StringView){.text = (char *)str, .length = strlen(str)}; } +size_t sv_chars_count(StringView *sv) { + if (sv == NULL) + return 0; + if (sv->text == NULL) + return 0; + size_t count = 0, i = 0; + while (i < sv->length) { + count++; + // Consulto si es un caracter ancho. + char cbyte = *(sv->text + i) & SV_START_WIDE_CHAR; + if (cbyte == (char)SV_START_WIDE_CHAR) { + // Consulto cuantos bytes tiene el caracter ancho. + char cbyte2 = *(sv->text + i) & SV_BYTES_2_WIDE_CHAR; + char cbyte3 = *(sv->text + i) & SV_BYTES_3_WIDE_CHAR; + char cbyte4 = *(sv->text + i) & SV_BYTES_4_WIDE_CHAR; + + if (cbyte4 == (char)SV_BYTES_4_WIDE_CHAR) { + i += 4; + continue; + } else if (cbyte3 == (char)SV_BYTES_3_WIDE_CHAR) { + i += 3; + continue; + } else if (cbyte2 == (char)SV_BYTES_2_WIDE_CHAR) { + i += 2; + continue; + } + } + i++; + } + return count; +} + void sv_left(StringView *sv, size_t l) { if (sv == NULL) { return; @@ -122,6 +140,26 @@ StringView sv_chop_by_delim(StringView *sv, char delim) { return returnsv; } +StringView sv_chop_by_type(StringView *sv, int (*checktype)(int)) { + StringView returnsv = SV_NULL; + size_t find = 0; + + while (find < sv->length && checktype(*(sv->text + find))) { + find++; + } + + if (find > sv->length) { + return returnsv; + } + returnsv.text = sv->text; + returnsv.length = find; + + sv->text += find + 1; + sv->length = sv->length - find; + + return returnsv; +} + bool sv_equals(StringView *sv1, StringView *sv2) { if (sv1 == NULL) return false; diff --git a/c/stringview/stringv.h b/c/stringview/stringv.h index cbc8f9a..5942f11 100644 --- a/c/stringview/stringv.h +++ b/c/stringview/stringv.h @@ -4,25 +4,209 @@ #include #include +/** + * @brief A non-owning view of a string + * + * @details Represents a contiguous sequence of characters defined by a pointer + * and a byte length. It does not own the memory buffer, so the underlying + * source string must remain valid while the StringView is in use. + */ + typedef struct StringView { char *text; size_t length; } StringView; +/** + * @brief Constant macro representing an empty or null StringView + * + * @details Useful for initializing or returning an invalid/empty view safely. + */ #define SV_NULL (StringView){.text = NULL, .length = 0} +/** + * @brief Bit masks and flags for UTF-8 wide character parsing + * + * @details These masks are used to examine the bit patterns of UTF-8 bytes + * in order to distinguish between standard ASCII characters and multibyte + * sequences (2, 3, or 4 bytes), as well as continuation bytes. + */ +enum StringViewWideFlags { + SV_START_WIDE_CHAR = 0x80, //< Bit mask to check if a byte belongs to a + //< multibyte sequence (10000000) + SV_BYTES_2_WIDE_CHAR = 0xC0, //< Pattern/mask for the start of a 2-byte + //< UTF-8 character (11000000) + SV_BYTES_3_WIDE_CHAR = 0xE0, //< Pattern/mask for the start of a 3-byte + //< UTF-8 character (11100000) + SV_BYTES_4_WIDE_CHAR = 0xF0, //< Pattern/mask for the start of a 4-byte + //< UTF-8 character (11110000) + SV_CONTINUE_WIDE_CHAR = + 0x80 //< Pattern/mask for a UTF-8 continuation byte (10xxxxxx) +}; + +/* + * @brief Create a string view from a const char* and set their length + * + * @details This string view provides a view of a string. It does not allocate + * memory, therefore, it lives only while the source is alive. The + * length is a bytes number of the string. It does not represents the number of + * characters. + * + * @param str pointer from the original string. It can be NULL. + * + * @return a StringView with the view of original string with corresponding + * length. + */ + StringView sv_from_cstr(const char *); +/** + * @brief Count the number of logical characters in the string view + * + * @details Traverses the string view bytes while properly handling UTF-8 wide + * characters. It counts each logical character exactly once, regardless of + * whether it occupies 1, 2, 3, or 4 physical bytes. Returns 0 if the pointer + * or internal text is NULL. + * + * @param sv pointer to the StringView to evaluate. + * + * @return the total number of logical characters in the string view. + */ +size_t sv_chars_count(StringView *); + +/** + * @brief Reduce the string view to its first 'l' bytes + * + * @details Modifies the string view in place, shortening its length to the + * specified number of bytes from the beginning. + * + * @param sv pointer to the StringView to modify. + * @param l number of bytes to keep from the left. + */ void sv_left(StringView *sv, size_t l); + +/** + * @brief Reduce the string view to its last 'l' bytes + * + * @details Modifies the string view in place, adjusting its start pointer and + * length to keep only the specified number of bytes from the end. + * + * @param sv pointer to the StringView to modify. + * @param l number of bytes to keep from the right. + */ void sv_right(StringView *sv, size_t l); +/** + * @brief Trim leading whitespace from the string view + * + * @details Modifies the string view in place by advancing the internal text + * pointer and decreasing the length to skip any whitespace characters at the + * beginning. + * + * @param sv pointer to the StringView to trim. + */ void sv_ltrim(StringView *sv); + +/** + * @brief Trim trailing whitespace from the string view + * + * @details Modifies the string view in place by decreasing its length to remove + * any whitespace characters at the end. + * + * @param sv pointer to the StringView to trim. + */ void sv_rtrim(StringView *sv); + +/** + * @brief Trim leading and trailing whitespace from the string view + * + * @details Combines left and right trimming to remove all surrounding + * whitespace characters from the string view in place. + * + * @param sv pointer to the StringView to trim. + */ void sv_trim(StringView *sv); +/** + * @brief Find the first occurrence of a character in the string view + * + * @details Searches sequentially through the bytes of the string view for the + * specified character. + * + * @param sv the StringView to search within (passed by value). + * @param c the character to look for. + * + * @return the index of the character if found, or -1 if the character is not + * present. + */ int sv_index_of(StringView sv, char c); + +/** + * @brief Split the string view at the first occurrence of a delimiter + * + * @details Modifies the original string view by advancing past the delimiter, + * and returns a new StringView representing the portion before the delimiter. + * + * @param sv pointer to the StringView to chop. + * @param delim the delimiter character used for splitting. + * + * @return a StringView containing the prefix up to the delimiter. + */ StringView sv_chop_by_delim(StringView *sv, char delim); + +/** + * @brief Chop the string view based on a custom character classification + * function + * + * @details Advances through the string view while the provided classification + * function returns true for characters, then modifies the original string view + * and returns the chopped prefix. + * + * @param sv pointer to the StringView to chop. + * @param checktype function pointer to classify characters (e.g., isspace, + * isdigit). + * + * @return a StringView representing the chopped portion matching the condition. + */ +StringView sv_chop_by_type(StringView *sv, int (*checktype)(int)); + +/** + * @brief Check if two string views are equal + * + * @details Compares the lengths and byte contents of two string views to + * determine if they are identical. + * + * @param sv1 pointer to the first StringView. + * @param sv2 pointer to the second StringView. + * + * @return true if both string views match in length and content, false + * otherwise. + */ bool sv_equals(StringView *sv1, StringView *sv2); + +/** + * @brief Check if a string view starts with a specific prefix + * + * @details Determines whether the given string view begins with the character + * sequence contained in the prefix string view. + * + * @param sv pointer to the main StringView. + * @param prefix pointer to the StringView representing the prefix to check. + * + * @return true if sv starts with prefix, false otherwise. + */ bool sv_starts_with(StringView *sv, StringView *prefix); + +/** + * @brief Check if a string view ends with a specific suffix + * + * @details Determines whether the given string view finishes with the character + * sequence contained in the suffix string view. + * + * @param sv pointer to the main StringView. + * @param suffix pointer to the StringView representing the suffix to check. + * + * @return true if sv ends with suffix, false otherwise. + */ bool sv_ends_with(StringView *sv, StringView *suffix); #endif