Mercurial > sdl-ios-xcode
comparison src/stdlib/SDL_string.c @ 4754:2072fed2f583
Added SDL_utf8strlcpy to copy at UTF-8 character boundaries.
Changed SDL_SendKeyboardText and SDL_SendEditingText to use SDL_utf8strlcpy.
author | dewyatt |
---|---|
date | Tue, 13 Jul 2010 15:05:45 -0400 |
parents | f7b03b6838cb |
children | e2f4e31b41fc |
comparison
equal
deleted
inserted
replaced
4753:11b0a6a3eb4d | 4754:2072fed2f583 |
---|---|
26 #include "SDL_stdinc.h" | 26 #include "SDL_stdinc.h" |
27 | 27 |
28 | 28 |
29 #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) | 29 #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) |
30 #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) | 30 #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) |
31 | |
32 #define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4) | |
33 #define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF) | |
34 | |
35 int UTF8_TrailingBytes(unsigned char c) | |
36 { | |
37 if (c >= 0xC0 && c<= 0xDF) | |
38 return 1; | |
39 else if (c >= 0xE0 && c <= 0xEF) | |
40 return 2; | |
41 else if (c >= 0xF0 && c <= 0xF4) | |
42 return 3; | |
43 else | |
44 return 0; | |
45 } | |
31 | 46 |
32 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) | 47 #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) |
33 static size_t | 48 static size_t |
34 SDL_ScanLong(const char *text, int radix, long *valuep) | 49 SDL_ScanLong(const char *text, int radix, long *valuep) |
35 { | 50 { |
359 dst[len] = '\0'; | 374 dst[len] = '\0'; |
360 } | 375 } |
361 return srclen; | 376 return srclen; |
362 } | 377 } |
363 #endif | 378 #endif |
379 | |
380 size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes) | |
381 { | |
382 size_t src_bytes = SDL_strlen(src); | |
383 size_t bytes = SDL_min(src_bytes, dst_bytes - 1); | |
384 int i = 0; | |
385 char trailing_bytes = 0; | |
386 if (bytes) | |
387 { | |
388 unsigned char c = (unsigned char)src[bytes - 1]; | |
389 if (UTF8_IsLeadByte(c)) | |
390 --bytes; | |
391 else if (UTF8_IsTrailingByte(c)) | |
392 { | |
393 for (i = bytes - 1; i != 0; --i) | |
394 { | |
395 c = (unsigned char)src[i]; | |
396 trailing_bytes = UTF8_TrailingBytes(c); | |
397 if (trailing_bytes) | |
398 { | |
399 if (bytes - i != trailing_bytes + 1) | |
400 bytes = i; | |
401 | |
402 break; | |
403 } | |
404 } | |
405 } | |
406 SDL_memcpy(dst, src, bytes); | |
407 } | |
408 dst[bytes] = '\0'; | |
409 return bytes; | |
410 } | |
364 | 411 |
365 #ifndef HAVE_STRLCAT | 412 #ifndef HAVE_STRLCAT |
366 size_t | 413 size_t |
367 SDL_strlcat(char *dst, const char *src, size_t maxlen) | 414 SDL_strlcat(char *dst, const char *src, size_t maxlen) |
368 { | 415 { |