summaryrefslogtreecommitdiff
path: root/utf8.c
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2000-11-18 22:50:28 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2000-11-18 22:50:28 +0000
commitb76347f2eb34c85a0a38543b2f57ca474fedab4d (patch)
tree31cc323c41bcb1ac42ff13659898c6589d43cf19 /utf8.c
parent5a2dd41731da4a994683b80fc75b362161912f47 (diff)
downloadperl-b76347f2eb34c85a0a38543b2f57ca474fedab4d.tar.gz
Introduce Perl_utf8_length(). Use it.
p4raw-id: //depot/perl@7744
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/utf8.c b/utf8.c
index f1b80a43b7..fc625dc464 100644
--- a/utf8.c
+++ b/utf8.c
@@ -353,6 +353,35 @@ Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
return Perl_utf8_to_uv(aTHX_ s, (STRLEN)-1, retlen, 0);
}
+/*
+=for apidoc|utf8_length|U8 *s|U8 *e
+
+Return the length of the UTF-8 char encoded string C<s> in characters.
+Stops at string C<e>. If C<e E<lt> s> or if the scan would end up
+past C<e>, return -1.
+
+=cut
+*/
+
+STRLEN
+Perl_utf8_length(pTHX_ U8* s, U8* e)
+{
+ STRLEN len = 0;
+
+ if (e < s)
+ return -1;
+ while (s < e) {
+ STRLEN t = UTF8SKIP(s);
+
+ if (e - s < t)
+ return -1;
+ s += t;
+ len++;
+ }
+
+ return len;
+}
+
/* utf8_distance(a,b) returns the number of UTF8 characters between
the pointers a and b */