summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2015-08-18 07:36:48 +0200
committerSébastien Helleu <flashcode@flashtux.org>2015-08-18 07:36:48 +0200
commit46a9d17ac331b1df9fbd33a7c23f87fd58246a2e (patch)
tree1a4bd97bade2b4977683fa2c07f639ace3f5e5c0 /src/core
parentfd1886e883968dbf372157644ce5664e75a6ff4b (diff)
downloadweechat-46a9d17ac331b1df9fbd33a7c23f87fd58246a2e.zip
api: add argument "length" in function utf8_is_valid()
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-string.c2
-rw-r--r--src/core/wee-utf8.c19
-rw-r--r--src/core/wee-utf8.h2
3 files changed, 15 insertions, 8 deletions
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index e27603512..2f1adb7d9 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -2307,7 +2307,7 @@ string_iconv_to_internal (const char *charset, const char *string)
if (local_utf8 && (!charset || !charset[0]))
return input;
- if (utf8_has_8bits (input) && utf8_is_valid (input, NULL))
+ if (utf8_has_8bits (input) && utf8_is_valid (input, -1, NULL))
return input;
output = string_iconv (0,
diff --git a/src/core/wee-utf8.c b/src/core/wee-utf8.c
index afa64d951..7cfb8a875 100644
--- a/src/core/wee-utf8.c
+++ b/src/core/wee-utf8.c
@@ -70,18 +70,24 @@ utf8_has_8bits (const char *string)
/*
* Checks if a string is UTF-8 valid.
*
+ * If length is <= 0, checks whole string.
+ * If length is > 0, checks only this number of chars (not bytes).
+ *
* Returns:
* 1: string is UTF-8 valid
- * 0: string it not UTF-8 valid, and then if error is not NULL, it is set with
- * first non valid UTF-8 char in string
+ * 0: string it not UTF-8 valid, and then if error is not NULL, it is set
+ * with first non valid UTF-8 char in string
*/
int
-utf8_is_valid (const char *string, char **error)
+utf8_is_valid (const char *string, int length, char **error)
{
- int code_point;
+ int code_point, current_char;
- while (string && string[0])
+ current_char = 0;
+
+ while (string && string[0]
+ && ((length <= 0) || (current_char < length)))
{
/*
* UTF-8, 2 bytes, should be: 110vvvvv 10vvvvvv
@@ -142,6 +148,7 @@ utf8_is_valid (const char *string, char **error)
goto invalid;
else
string++;
+ current_char++;
}
if (error)
*error = NULL;
@@ -165,7 +172,7 @@ utf8_normalize (char *string, char replacement)
while (string && string[0])
{
- if (utf8_is_valid (string, &error))
+ if (utf8_is_valid (string, -1, &error))
return;
error[0] = replacement;
string = error + 1;
diff --git a/src/core/wee-utf8.h b/src/core/wee-utf8.h
index 8e256c9f7..abdf96b2b 100644
--- a/src/core/wee-utf8.h
+++ b/src/core/wee-utf8.h
@@ -30,7 +30,7 @@ extern int local_utf8;
extern void utf8_init ();
extern int utf8_has_8bits (const char *string);
-extern int utf8_is_valid (const char *string, char **error);
+extern int utf8_is_valid (const char *string, int length, char **error);
extern void utf8_normalize (char *string, char replacement);
extern const char *utf8_prev_char (const char *string_start,
const char *string);