From 0f448197aa5c08a75e91d56ed2f9acde3e132f64 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 14 Feb 2003 09:27:35 +0000 Subject: [PATCH] Sped up utf8_strlen(), introduced utf8_strnlen(). --- charset/strlen.c | 28 +++++++++++++++++++--------- charset/unicode.h | 3 ++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/charset/strlen.c b/charset/strlen.c index 39c1cb5b..1fb0f756 100644 --- a/charset/strlen.c +++ b/charset/strlen.c @@ -1,7 +1,7 @@ /* * The UniCode Library -- String Length * - * (c) 1997 Martin Mares + * (c) 1997--2003 Martin Mares * (c) 2003 Robert Spalek * * This software may be freely distributed and used according to the terms @@ -25,13 +25,23 @@ uns utf8_strlen(byte *str) { uns len = 0; - while (1) - { - uns c; - GET_UTF8(str, c); - if (!c) - return len; - len++; - } + while (*str) + { + UTF8_SKIP(str); + len++; + } + return len; } +uns +utf8_strnlen(byte *str, uns n) +{ + uns len = 0; + byte *end = str + n; + while (str < end) + { + UTF8_SKIP(str); + len++; + } + return len; +} diff --git a/charset/unicode.h b/charset/unicode.h index 1e11e0f7..80c4ff88 100644 --- a/charset/unicode.h +++ b/charset/unicode.h @@ -1,7 +1,7 @@ /* * The UniCode Library * - * (c) 1997 Martin Mares + * (c) 1997--2003 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -119,5 +119,6 @@ uns utf8_to_ucs2(word *, byte *); byte *static_ucs2_to_utf8(word *); uns Ustrlen(word *); uns utf8_strlen(byte *str); +uns utf8_strnlen(byte *str, uns n); #endif -- 2.39.2