Revisão | 7414a4f6abb634fe6135a9f136cb0774e9714f51 (tree) |
---|---|
Hora | 2012-01-21 00:43:13 |
Autor | Moriguchi, Hirokazu <umorigu@gmai...> |
Commiter | Moriguchi, Hirokazu |
MessageUtil_GetUTF8StaticBinaryBlock: Find key string by bsearch()
@@ -4,7 +4,7 @@ | ||
4 | 4 | /** |
5 | 5 | * アドレス固定のWide文字列とそれに対応するUTF-8文字列を格納する構造体 |
6 | 6 | */ |
7 | -typedef struct { | |
7 | +typedef struct StrPair_ { | |
8 | 8 | const wchar_t *ws; |
9 | 9 | char *u8s; |
10 | 10 | size_t u8size; |
@@ -22,6 +22,9 @@ static int strMapCount = 0; | ||
22 | 22 | //! 確保済みのStrPairの領域数 |
23 | 23 | static int strMapMaxCount = 0; |
24 | 24 | |
25 | +//! StrPair比較関数 | |
26 | +static int CompareStrPair(const void *c1, const void *c2); | |
27 | + | |
25 | 28 | /** |
26 | 29 | * staticなWide文字列に対応するUTF-8バイナリ文字列領域を確保し、その先頭アドレスを返す |
27 | 30 | */ |
@@ -37,12 +40,13 @@ const char* const MessageUtil_GetUTF8StaticBinaryBlock(const wchar_t* const ws, | ||
37 | 40 | g_initialized = TRUE; |
38 | 41 | } |
39 | 42 | EnterCriticalSection(&g_msgUtilLLock); |
40 | - for (i = 0; i < strMapCount; i++) | |
41 | 43 | { |
42 | - if (pStrMap[i].ws == ws) | |
44 | + // 二分探索 | |
45 | + StrPair keyPair = {ws, NULL}; | |
46 | + StrPair *pResultStrPair = bsearch(&keyPair, pStrMap, strMapCount, sizeof(StrPair), CompareStrPair); | |
47 | + if (pResultStrPair) | |
43 | 48 | { |
44 | - pResult = pStrMap[i].u8s; | |
45 | - break; | |
49 | + pResult = pResultStrPair->u8s; | |
46 | 50 | } |
47 | 51 | } |
48 | 52 | if (pResult == NULL) |
@@ -74,6 +78,9 @@ const char* const MessageUtil_GetUTF8StaticBinaryBlock(const wchar_t* const ws, | ||
74 | 78 | pStrMap[index].u8s = beginPos; |
75 | 79 | postSize = WideCharToMultiByte(CP_UTF8, 0, ws, ws_area_length, beginPos, newSize, NULL, NULL); |
76 | 80 | pResult = beginPos; |
81 | + | |
82 | + // pStrMap ソートする | |
83 | + qsort(pStrMap, strMapCount, sizeof(StrPair), CompareStrPair); | |
77 | 84 | } |
78 | 85 | else |
79 | 86 | { |
@@ -104,7 +111,15 @@ void MessageUtil_FreeUTF8StaticBinaryBlocks() | ||
104 | 111 | if (pStrMap) |
105 | 112 | { |
106 | 113 | free(pStrMap); |
107 | - pStrMap = (StrPair*)NULL; | |
114 | + pStrMap = NULL; | |
108 | 115 | } |
116 | + strMapCount = 0; | |
109 | 117 | LeaveCriticalSection(&g_msgUtilLLock); |
110 | 118 | } |
119 | + | |
120 | +static int CompareStrPair(const void *c1, const void *c2) | |
121 | +{ | |
122 | + StrPair *p1 = (StrPair*)c1; | |
123 | + StrPair *p2 = (StrPair*)c2; | |
124 | + return p1->ws - p2->ws; | |
125 | +} |