shithub: femtolisp

Download patch

ref: ab876b803ac6efb812fddc8a154202b80632eb5e
parent: 40794e7a0a6c15a51ba7a781374fc0de9b529ec6
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Wed Oct 23 12:08:45 EDT 2024

u8_reverse: shorten

--- a/utf8.c
+++ b/utf8.c
@@ -285,40 +285,36 @@
 }
 
 int
-u8_reverse(char *dest, char * src, size_t len)
+u8_reverse(char *dest, char *src, size_t len)
 {
-	size_t si = 0, di = len;
-	uint8_t c;
+	size_t si, di;
 
-	dest[di] = '\0';
-	while(si < len){
-		c = (uint8_t)src[si];
-		if((~c) & 0x80){
+	dest[di = len] = '\0';
+	for(si = 0; si < len;){
+		switch((uint8_t)src[si]>>4){
+		case 0xf:
+			di -= 4;
+			dest[di+0] = src[si++];
+			dest[di+1] = src[si++];
+			dest[di+2] = src[si++];
+			dest[di+3] = src[si++];
+			break;
+		case 0xe:
+			di -= 3;
+			dest[di+0] = src[si++];
+			dest[di+1] = src[si++];
+			dest[di+2] = src[si++];
+			break;
+		case 0xd:
+		case 0xc:
+			di -= 2;
+			dest[di+0] = src[si++];
+			dest[di+1] = src[si++];
+			break;
+		default:
 			di--;
-			dest[di] = c;
-			si++;
-		}else{
-			switch(c>>4){
-			case 0xc:
-			case 0xd:
-				di -= 2;
-				*((uint16_t*)&dest[di]) = *((uint16_t*)&src[si]);
-				si += 2;
-				break;
-			case 0xe:
-				di -= 3;
-				dest[di] = src[si];
-				*((uint16_t*)&dest[di+1]) = *((uint16_t*)&src[si+1]);
-				si += 3;
-				break;
-			case 0xf:
-				di -= 4;
-				*((uint32_t*)&dest[di]) = *((uint32_t*)&src[si]);
-				si += 4;
-				break;
-			default:
-				return 1;
-			}
+			dest[di+0] = src[si++];
+			break;
 		}
 	}
 	return 0;