OSDN Git Service

libtest:ライブラリテスト用ソースを追加。
authorhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 25 Feb 2013 09:51:44 +0000 (18:51 +0900)
committerhikarupsp <hikarupsp@users.sourceforge.jp>
Mon, 25 Feb 2013 09:51:44 +0000 (18:51 +0900)
chnlib:CHNLIB_UIPArray_GetSeparatedUTF8Characterを追加。

CHNOSProject/chn/chnlib.c
CHNOSProject/chn/chnlib.h
CHNOSProject/chn/chnlib02.c
CHNOSProject/chn/chnlib04.c
CHNOSProject/libtest/libtest/main.c [new file with mode: 0644]

index 1b96da3..cd8729c 100755 (executable)
@@ -180,7 +180,7 @@ void CHNLIB_Debug_Internal_PrintStructureDataSub(void *structure, uint level)
             for(j = 0; j < i; j++){\r
                 p = CHNLIB_UIPArray_GetPointerByIndex(structure, j);\r
                 CHNLIB_Debug_Internal_PrintIndent(level);\r
-                CHNLIB_Debug("%s[%p]:[%d]=(%d,[%p])", "", 0, "", CHNLIB_Debug_Internal_GetStructTypeNameByID(typeid), structure, j, CHNLIB_UIPArray_GetData32ByIndex(structure, j), p);\r
+                CHNLIB_Debug("%s[%p]:[%d]=(%u,[%p])", "", 0, "", CHNLIB_Debug_Internal_GetStructTypeNameByID(typeid), structure, j, CHNLIB_UIPArray_GetData32ByIndex(structure, j), p);\r
                 if(CHNLIB_Debug_PrintStructureData_RecursiveCounter != 0){\r
                     CHNLIB_Debug_Internal_PrintStructureDataSub(p, level + 1);\r
                 }\r
index 3bca6ed..6d9c68d 100755 (executable)
@@ -113,6 +113,7 @@ uint CHNLIB_CString_GetCountOfContain(const char s[], const char search[]);
 //@chnlib02.c\r
 int CHNLIB_String_Search_UIPArrayStringLocation(const CHNLIB_String *s, int s_start, const CHNLIB_UIPArray *list, int *location);\r
 int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, const CHNLIB_UIPArray *list, const CHNLIB_String *s);\r
+int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const CHNLIB_String *s);\r
 CHNLIB_String *CHNLIB_ReadLine(FILE *fp);\r
 \r
 //@chnlib03.c\r
index e37e969..160ecab 100644 (file)
@@ -93,6 +93,58 @@ int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, co
     return 0;\r
 }\r
 \r
+int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const CHNLIB_String *s){\r
+    //文字列sを、UTF-8の一文字ごとに分割し、その文字のUnicodeをdata32、その一文字に該当するStringをpointerに格納し、separatedに追加する形で返す。\r
+    //不完全なUTF-8文字列は無視される。\r
+    const char *refstr;\r
+    int i, i_max, type;\r
+    int phase, start;\r
+    uint unicode;\r
+    \r
+    if(separated == NULL || CHNLIB_StructureHeader_GetTypeID(s) != CHNLIB_STRUCT_ID_String){\r
+        return 1;\r
+    }\r
+    \r
+    refstr = CHNLIB_String_GetReferencePointerOfCString(s);\r
+    i_max = CHNLIB_String_GetLength(s);\r
+    \r
+    phase = 0;\r
+    unicode = 0;\r
+    for(i = 0; i < i_max; i++){\r
+        type = CHNLIB_UTF8_GetCharacterType(refstr[i]);\r
+        switch (type) {\r
+            case 1:\r
+                CHNLIB_UIPArray_AppendLast(separated, refstr[i], CHNLIB_String_ExtractByLength(s, i, i));\r
+                phase = 0;\r
+                unicode = 0;\r
+                break;\r
+                \r
+            case 0:\r
+                if(phase > 0){\r
+                    unicode <<= 6;\r
+                    unicode |= (refstr[i] & 0x3f);\r
+                    phase--;\r
+                    if(phase == 0){\r
+                        //一文字完成\r
+                        CHNLIB_UIPArray_AppendLast(separated, unicode, CHNLIB_String_ExtractByLength(s, start, i - start + 1));\r
+                    }\r
+                }\r
+                break;\r
+                \r
+            case 2:\r
+            case 3:\r
+            case 4:\r
+                start = i;\r
+                unicode = (refstr[i] << (type + 1)) >> (type + 1);\r
+                phase = type - 1;\r
+                break;\r
+        }\r
+    }\r
+    \r
+    return 0;\r
+}\r
+\r
+\r
 CHNLIB_String *CHNLIB_ReadLine(FILE *fp)\r
 {\r
     char s[CHNLIB_MAX_STRING_LENGTH];\r
index b4a4576..7b05572 100644 (file)
 \r
 int CHNLIB_UTF8_GetCharacterType(char c)\r
 {\r
+    //UTF-8文字列中の1バイトcが、UTF-8文字列中でどのような役割を持つのかを返す。\r
     if(((c >> 6) & 3) == 2){\r
         //マルチバイト後続バイト\r
+        //10xxxxxx\r
         return 0;\r
     } else if(((c >> 7) & 1) == 0){\r
         //1Byte\r
+        //7bit\r
+        //0xxxxxxx\r
         return 1;\r
     } else if(((c >> 5) & 7) == 6){\r
         //2Byte\r
+        //11bit\r
+        //110xxxxx\r
         return 2;\r
     } else if(((c >> 4) & 15) == 14){\r
         //3Byte\r
+        //16bit\r
+        //1110xxxx\r
         return 3;\r
     } else if(((c >> 3) & 31) == 30){\r
         //4Byte\r
+        //21bit\r
+        //11110xxx\r
         return 4;\r
     }\r
     \r
     return 0;\r
-}
\ No newline at end of file
+}\r
+\r
+\r
+\r
diff --git a/CHNOSProject/libtest/libtest/main.c b/CHNOSProject/libtest/libtest/main.c
new file mode 100644 (file)
index 0000000..914c72d
--- /dev/null
@@ -0,0 +1,25 @@
+//
+//  main.c
+//  libtest
+//
+//  Created by 西田 耀 on 13/02/25.
+//  Copyright (c) 2013年 Hikaru Nishida. All rights reserved.
+//
+
+#include <stdio.h>
+#include "chnlib.h"
+
+int main(int argc, const char * argv[])
+{
+    CHNLIB_String *s;
+    CHNLIB_UIPArray *separated;
+    
+    separated = CHNLIB_UIPArray_Initialize();
+    s = CHNLIB_String_Initialize("あいうえお漢字。");
+    
+    CHNLIB_UIPArray_GetSeparatedUTF8Character(&separated, s);
+    CHNLIB_Debug_PrintStructureData(separated, 0);
+    
+    return 0;
+}
+