Commit de77b3b3 authored by Chris Müller's avatar Chris Müller

lib: rename utf8 functions for shorter names (chr,rchr,size,len,str).

parent 2fc7ef84
......@@ -177,7 +177,7 @@
byte*
cry_utf8_strchr(const byte* str, unicode character)
cry_utf8_chr(const byte* str, unicode character)
{
assert(str != 0 && (UTF8_IS_SINGLE(*str) || UTF8_IS_LEAD(*str)));
unicode ch = 0;
......@@ -197,7 +197,7 @@ cry_utf8_strchr(const byte* str, unicode character)
byte*
cry_utf8_strrchr(const byte* str, unicode character)
cry_utf8_rchr(const byte* str, unicode character)
{
assert(str != 0 && (UTF8_IS_SINGLE(*str) || UTF8_IS_LEAD(*str)));
......@@ -224,16 +224,36 @@ cry_utf8_strrchr(const byte* str, unicode character)
byte*
cry_utf8_strstr(const byte* str1, const byte* str2)
cry_utf8_str(const byte* str1, const byte* str2)
{
assert(str1 != 0 && (UTF8_IS_SINGLE(*str1) || UTF8_IS_LEAD(*str1)));
const byte* cp = str1;
const byte* s1 = 0;
const byte* s2 = 0;
if( !*str2 )
return cry_cast(byte*, str1);
while(*cp) {
s1 = cp;
s2 = str2;
while(*s1 && *s2 && !(*s1 - *s2))
s1++, s2++;
if( !*s2)
return cry_cast(byte*, cp);
++cp;
}
return 0;
}
size_t
cry_utf8_strlen(const byte* str)
cry_utf8_len(const byte* str)
{
assert(str != 0 && (UTF8_IS_SINGLE(*str) || UTF8_IS_LEAD(*str)));
......@@ -296,7 +316,7 @@ cry_utf8_prev(const byte* str)
size_t
cry_utf8_strsize(const byte* str)
cry_utf8_size(const byte* str)
{
assert(str != 0 && (UTF8_IS_SINGLE(*str) || UTF8_IS_LEAD(*str)));
......
......@@ -15,22 +15,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef CRYSTAL_UTF8_H
#define CRYSTAL_UTF8_H
#include "standard.h"
int cry_utf8_validate(const byte* str);
byte* cry_utf8_strchr(const byte* str, unicode character);
byte* cry_utf8_strrchr(const byte* str, unicode character);
byte* cry_utf8_strstr(const byte* str1, const byte* str2);
byte* cry_utf8_chr(const byte* str, unicode character);
byte* cry_utf8_rchr(const byte* str, unicode character);
byte* cry_utf8_str(const byte* str1, const byte* str2);
size_t cry_utf8_strsize(const byte* str);
size_t cry_utf8_strlen(const byte* str);
size_t cry_utf8_size(const byte* str);
size_t cry_utf8_len(const byte* str);
unicode cry_utf8_get(const byte* str);
size_t cry_utf8_codepoints(const byte* str);
byte* cry_utf8_next(const byte* str);
byte* cry_utf8_prev(const byte* str);
#endif // CRYSTAL_UTF8_H
......@@ -37,17 +37,17 @@ static void test_utf8_length(const_pointer data)
{
test_utf8_validate(data);
assert(cry_utf8_strlen(ascii) == 10);
assert(cry_utf8_strlen(latin1) == 16);
assert(cry_utf8_strlen(greek) == 4);
assert(cry_utf8_strlen(hiragana) == 4);
assert(cry_utf8_strlen(mixed) == 9);
assert(cry_utf8_strsize(ascii) == 10);
assert(cry_utf8_strsize(latin1) == 18);
assert(cry_utf8_strsize(greek) == 8);
assert(cry_utf8_strsize(hiragana) == 12);
assert(cry_utf8_strsize(mixed) == 17);
assert(cry_utf8_len(ascii) == 10);
assert(cry_utf8_len(latin1) == 16);
assert(cry_utf8_len(greek) == 4);
assert(cry_utf8_len(hiragana) == 4);
assert(cry_utf8_len(mixed) == 9);
assert(cry_utf8_size(ascii) == 10);
assert(cry_utf8_size(latin1) == 18);
assert(cry_utf8_size(greek) == 8);
assert(cry_utf8_size(hiragana) == 12);
assert(cry_utf8_size(mixed) == 17);
}
......@@ -92,20 +92,24 @@ static void test_utf8_search(const_pointer data)
test_utf8_validate(data);
// forward search
assert(cry_utf8_strchr(ascii, 's') == ascii + 2);
assert(cry_utf8_strchr(ascii, 'g') == ascii + 9);
assert(cry_utf8_strchr(mixed, 'z') == mixed + 1);
assert(cry_utf8_strchr(mixed, 0x00E4) == mixed + 2);
assert(cry_utf8_strchr(mixed, 0x4ED0) == mixed + 5);
assert(cry_utf8_strchr(mixed, 0xFEC5) == mixed + 14);
assert(cry_utf8_strchr(ascii, 'P') == 0);
assert(cry_utf8_chr(ascii, 's') == ascii + 2);
assert(cry_utf8_chr(ascii, 'g') == ascii + 9);
assert(cry_utf8_chr(mixed, 'z') == mixed + 1);
assert(cry_utf8_chr(mixed, 0x00E4) == mixed + 2);
assert(cry_utf8_chr(mixed, 0x4ED0) == mixed + 5);
assert(cry_utf8_chr(mixed, 0xFEC5) == mixed + 14);
assert(cry_utf8_chr(ascii, 'P') == 0);
// reverse search
assert(cry_utf8_strrchr(ascii, 's') == ascii + 4);
assert(cry_utf8_strrchr(latin1, 'G') == latin1);
assert(cry_utf8_strrchr(hiragana, 0x305C) == hiragana);
assert(cry_utf8_strrchr(mixed, 0x00E4) == mixed + 2);
assert(cry_utf8_strrchr(ascii, 'P') == 0);
assert(cry_utf8_rchr(ascii, 's') == ascii + 4);
assert(cry_utf8_rchr(latin1, 'G') == latin1);
assert(cry_utf8_rchr(hiragana, 0x305C) == hiragana);
assert(cry_utf8_rchr(mixed, 0x00E4) == mixed + 2);
assert(cry_utf8_rchr(ascii, 'P') == 0);
//substrings
assert(cry_utf8_str(ascii, "string") == ascii + 4);
assert(cry_utf8_str(ascii, "nge") == 0);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment