develooper Front page | perl.perl5.porters | Postings from September 2000

[PATCH] More error handling on utf8_to_bytes

From:
Simon Cozens
Date:
September 14, 2000 05:07
Subject:
[PATCH] More error handling on utf8_to_bytes
Message ID:
20000914130649.A20472@deep-dark-truthful-mirror.perlhacker.org
utf8_to_bytes returns zero on failure. Which is fine, unless the character
really was zero; I set len to -1 as well so you really do know if there was an
error.

Differences ...

==== //depot/bleadperl/utf8.c#3 (text) ====
Index: perl/utf8.c
--- perl/utf8.c.~1~	Thu Sep 14 13:05:35 2000
+++ perl/utf8.c	Thu Sep 14 13:05:35 2000
@@ -280,7 +280,7 @@
 Converts a string C<s> of length C<len> from UTF8 into byte encoding.
 Unlike C<bytes_to_utf8>, this over-writes the original string, and
 updates len to contain the new length.
-Returns zero on failure leaving the string and len unchanged
+Returns zero on failure, setting C<len> to -1.
 
 =cut
 */
@@ -300,8 +300,10 @@
     while (s < send) {
 	U8 c = *s++;
         if (c >= 0x80 &&
-	    ( (s >= send) || ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2)))
+	    ( (s >= send) || ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
+	    *len = -1;
 	    return 0;    
+	}
     }
     s = save;
     while (s < send) {
End of Patch.

-- 
Behind every great computer sits a skinny little geek.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About