[U-Boot] [PATCH 12/15] lib: charset: upper/lower case conversion

Heinrich Schuchardt xypron.glpk at gmx.de
Sat Aug 11 15:28:17 UTC 2018


Provide functions for upper and lower case conversion.

Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
---
 include/charset.h | 16 ++++++++++++++++
 lib/charset.c     | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/include/charset.h b/include/charset.h
index 1c0976dde6..6fdfaa9579 100644
--- a/include/charset.h
+++ b/include/charset.h
@@ -142,6 +142,22 @@ int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count);
  */
 #define utf16_utf8_strcpy(d, s) utf16_utf8_strncpy((d), (s), SIZE_MAX)
 
+/**
+ * utf_to_lower() - convert a Unicode letter to lower case
+ *
+ * @code:		letter to convert
+ * Return:		lower case letter or unchanged letter
+ */
+s32 utf_to_lower(const s32 code);
+
+/**
+ * utf_to_upper() - convert a Unicode letter to upper case
+ *
+ * @code:		letter to convert
+ * Return:		upper case letter or unchanged letter
+ */
+s32 utf_to_upper(const s32 code);
+
 /**
  * u16_strlen - count non-zero words
  *
diff --git a/lib/charset.c b/lib/charset.c
index d2c723be3c..6531b56e85 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -6,8 +6,12 @@
  */
 
 #include <charset.h>
+#include <capitalization.h>
 #include <malloc.h>
 
+static struct capitalization_table capitalization_table[] =
+	UNICODE_CAPITALIZATION_TABLE;
+
 s32 utf8_get(const char **src)
 {
 	s32 code = 0;
@@ -248,6 +252,43 @@ int utf16_utf8_strncpy(char **dst, const u16 *src, size_t count)
 	return 0;
 }
 
+s32 utf_to_lower(const s32 code)
+{
+	struct capitalization_table *pos = capitalization_table;
+	s32 ret = code;
+
+	if (code <= 0x7f) {
+		if (code >= 'A' && code <= 'Z')
+			ret += 0x20;
+		return ret;
+	}
+	for (; pos->upper; ++pos) {
+		if (pos->upper == code) {
+			ret = pos->lower;
+			break;
+		}
+	}
+	return ret;
+}
+
+s32 utf_to_upper(const s32 code)
+{
+	struct capitalization_table *pos = capitalization_table;
+	s32 ret = code;
+
+	if (code <= 0x7f) {
+		if (code >= 'a' && code <= 'z')
+			ret -= 0x20;
+		return ret;
+	}
+	for (; pos->lower; ++pos) {
+		if (pos->lower == code) {
+			ret = pos->upper;
+			break;
+		}
+	}
+	return ret;
+}
 
 size_t u16_strlen(const u16 *in)
 {
-- 
2.18.0



More information about the U-Boot mailing list