CHNOSProjectのすべてのプロジェクトのソースのルート
Revisión | 1ba1ff02213d50410430f7c01c6ec362ad3d8aca (tree) |
---|---|
Tiempo | 2013-06-29 00:57:45 |
Autor | hikarupsp <hikarupsp@user...> |
Commiter | hikarupsp |
chnlib:構造体のハッシュ値取得を実装。検索の高速化に利用する予定。文字列の連結・コピーも実装。合わせてlibtestも更新。
@@ -7,13 +7,6 @@ | ||
7 | 7 | objects = { |
8 | 8 | |
9 | 9 | /* Begin PBXContainerItemProxy section */ |
10 | - 177F343E17339E2F004D6778 /* PBXContainerItemProxy */ = { | |
11 | - isa = PBXContainerItemProxy; | |
12 | - containerPortal = 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */; | |
13 | - proxyType = 2; | |
14 | - remoteGlobalIDString = 177F33E3172A4D98004D6778; | |
15 | - remoteInfo = chnl; | |
16 | - }; | |
17 | 10 | 1783027F1719927500FF22B2 /* PBXContainerItemProxy */ = { |
18 | 11 | isa = PBXContainerItemProxy; |
19 | 12 | containerPortal = 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */; |
@@ -59,7 +52,7 @@ | ||
59 | 52 | /* End PBXContainerItemProxy section */ |
60 | 53 | |
61 | 54 | /* Begin PBXFileReference section */ |
62 | - 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ctoolset.xcodeproj; path = ctoolset/ctoolset.xcodeproj; sourceTree = "<group>"; }; | |
55 | + 17677F96176B838900F12210 /* chnos.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = chnos.xcodeproj; path = chnos/chnos.xcodeproj; sourceTree = "<group>"; }; | |
63 | 56 | 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SimpleHTMLEditor.xcodeproj; path = SimpleHTMLEditor/SimpleHTMLEditor.xcodeproj; sourceTree = "<group>"; }; |
64 | 57 | 17D0F3BD16DA2F3700C62B79 /* AI002.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AI002.xcodeproj; path = AI002/AI002.xcodeproj; sourceTree = "<group>"; }; |
65 | 58 | 17D0F3C316DA2F3C00C62B79 /* AI003.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AI003.xcodeproj; path = AI003/AI003.xcodeproj; sourceTree = "<group>"; }; |
@@ -72,7 +65,7 @@ | ||
72 | 65 | 1724D6D416DA113E008A8FE2 = { |
73 | 66 | isa = PBXGroup; |
74 | 67 | children = ( |
75 | - 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */, | |
68 | + 17677F96176B838900F12210 /* chnos.xcodeproj */, | |
76 | 69 | 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */, |
77 | 70 | 17EE90AA16DB67DB005238B2 /* libtest.xcodeproj */, |
78 | 71 | 17D0F42816DA37C400C62B79 /* c2wiki.xcodeproj */, |
@@ -82,10 +75,9 @@ | ||
82 | 75 | ); |
83 | 76 | sourceTree = "<group>"; |
84 | 77 | }; |
85 | - 177F33DA172A4B05004D6778 /* Products */ = { | |
78 | + 17677F97176B838900F12210 /* Products */ = { | |
86 | 79 | isa = PBXGroup; |
87 | 80 | children = ( |
88 | - 177F343F17339E2F004D6778 /* chnl */, | |
89 | 81 | ); |
90 | 82 | name = Products; |
91 | 83 | sourceTree = "<group>"; |
@@ -173,8 +165,8 @@ | ||
173 | 165 | ProjectRef = 17D0F3F416DA305000C62B79 /* chn.xcodeproj */; |
174 | 166 | }, |
175 | 167 | { |
176 | - ProductGroup = 177F33DA172A4B05004D6778 /* Products */; | |
177 | - ProjectRef = 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */; | |
168 | + ProductGroup = 17677F97176B838900F12210 /* Products */; | |
169 | + ProjectRef = 17677F96176B838900F12210 /* chnos.xcodeproj */; | |
178 | 170 | }, |
179 | 171 | { |
180 | 172 | ProductGroup = 17EE90AB16DB67DB005238B2 /* Products */; |
@@ -192,13 +184,6 @@ | ||
192 | 184 | /* End PBXProject section */ |
193 | 185 | |
194 | 186 | /* Begin PBXReferenceProxy section */ |
195 | - 177F343F17339E2F004D6778 /* chnl */ = { | |
196 | - isa = PBXReferenceProxy; | |
197 | - fileType = "compiled.mach-o.executable"; | |
198 | - path = chnl; | |
199 | - remoteRef = 177F343E17339E2F004D6778 /* PBXContainerItemProxy */; | |
200 | - sourceTree = BUILT_PRODUCTS_DIR; | |
201 | - }; | |
202 | 187 | 178302801719927500FF22B2 /* SimpleHTMLEditor.app */ = { |
203 | 188 | isa = PBXReferenceProxy; |
204 | 189 | fileType = wrapper.application; |
@@ -96,14 +96,12 @@ | ||
96 | 96 | shouldBeEnabled = "No" |
97 | 97 | ignoreCount = "0" |
98 | 98 | continueAfterRunningActions = "No" |
99 | - filePath = "libtest/libtest/main.c" | |
100 | - timestampString = "390597364.50073" | |
99 | + filePath = "SimpleHTMLEditor/SimpleHTMLEditor/AppDelegate.h" | |
100 | + timestampString = "394127181.292504" | |
101 | 101 | startingColumnNumber = "9223372036854775807" |
102 | 102 | endingColumnNumber = "9223372036854775807" |
103 | - startingLineNumber = "28" | |
104 | - endingLineNumber = "28" | |
105 | - landmarkName = "main()" | |
106 | - landmarkType = "7"> | |
103 | + startingLineNumber = "8" | |
104 | + endingLineNumber = "8"> | |
107 | 105 | </FileBreakpoint> |
108 | 106 | </FileBreakpoints> |
109 | 107 | </Bucket> |
@@ -7,7 +7,7 @@ | ||
7 | 7 | <key>chn.xcscheme</key> |
8 | 8 | <dict> |
9 | 9 | <key>orderHint</key> |
10 | - <integer>1</integer> | |
10 | + <integer>2</integer> | |
11 | 11 | </dict> |
12 | 12 | </dict> |
13 | 13 | <key>SuppressBuildableAutocreation</key> |
@@ -108,6 +108,7 @@ void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint type | ||
108 | 108 | #endif |
109 | 109 | header->referenceCount = 1; |
110 | 110 | header->destructor = NULL; |
111 | + header->getHash = NULL; | |
111 | 112 | return; |
112 | 113 | } |
113 | 114 |
@@ -128,6 +129,24 @@ uint CHNLIB_StructureHeader_GetTypeID(const void *structure) | ||
128 | 129 | return CHNLIB_STRUCT_ID_Null; |
129 | 130 | } |
130 | 131 | |
132 | +uint CHNLIB_StructureHeader_GetHash(const void *structure) | |
133 | +{ | |
134 | + //structureが指す構造体のヘッダを確認して、その構造体定義のハッシュ値を返す。 | |
135 | + CHNLIB_StructureHeader *strhead; | |
136 | + | |
137 | + if(structure == NULL){ | |
138 | + return 0; | |
139 | + } | |
140 | + | |
141 | + strhead = (CHNLIB_StructureHeader *)structure; | |
142 | + if(strhead->signature == CHNLIB_STRUCTURE_SIGNATURE){ | |
143 | + if(strhead->getHash != NULL){ | |
144 | + return strhead->getHash(structure); | |
145 | + } | |
146 | + } | |
147 | + return 0; | |
148 | +} | |
149 | + | |
131 | 150 | void *CHNLIB_System_AllocateMemory_Strict(int size, const char filename[], int line, const char funcname[]) |
132 | 151 | { |
133 | 152 | //mallocを行い、確保が失敗した場合にメッセージを出力してプログラムを終了する。 |
@@ -46,6 +46,7 @@ struct CHNLIB_STRUCTURE_HEADER { | ||
46 | 46 | uint typeid; |
47 | 47 | uint referenceCount; |
48 | 48 | void (*destructor)(void **structrue); |
49 | + uint (*getHash)(const void *structure); | |
49 | 50 | }; |
50 | 51 | |
51 | 52 | // |
@@ -78,6 +79,7 @@ void CHNLIB_Debug_PrintStructureData(void *structure, uint level); | ||
78 | 79 | void CHNLIB_ReportError(const char format[], const char filename[], int line, const char funcname[], ...); //("", CHNLIB_DEBUG_ARGUMENTS); |
79 | 80 | void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint typeid); |
80 | 81 | uint CHNLIB_StructureHeader_GetTypeID(const void *structure); |
82 | +uint CHNLIB_StructureHeader_GetHash(const void *structure); | |
81 | 83 | void *CHNLIB_System_AllocateMemory_Strict(int size, const char filename[], int line, const char funcname[]); |
82 | 84 | void CHNLIB_System_FreeMemory(void *p, const char filename[], int line, const char funcname[]); |
83 | 85 | void CHNLIB_Retain(void **structure); |
@@ -117,25 +119,41 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB | ||
117 | 119 | //@chnlib01.c |
118 | 120 | typedef struct CHNLIB_STRING CHNLIB_String; |
119 | 121 | struct CHNLIB_STRING; |
122 | +//----文字列の初期化 | |
120 | 123 | CHNLIB_String *CHNLIB_String_Initialize(const char str[]); |
121 | 124 | CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...); |
125 | +//----文字列の解放 | |
122 | 126 | void CHNLIB_String_Free(CHNLIB_String *strtag); |
127 | +//----文字列の設定 | |
123 | 128 | int CHNLIB_String_SetStringFromCString(CHNLIB_String *strtag, const char s[]); |
129 | +//----文字列の表示 | |
124 | 130 | int CHNLIB_String_Print(CHNLIB_String *strtag); |
131 | +//----文字列の参照の取得 | |
125 | 132 | const char *CHNLIB_String_GetReferencePointerOfCString(const CHNLIB_String *strtag); |
133 | +//----文字列の切り出し | |
126 | 134 | CHNLIB_String *CHNLIB_String_ExtractByLength(const CHNLIB_String *strtag, int start, int len); |
135 | +//----文字列の連結 | |
136 | +CHNLIB_String *CHNLIB_String_Concatenate(const CHNLIB_String *s1, const CHNLIB_String *s2); | |
137 | +//----文字列の長さを取得 | |
127 | 138 | int CHNLIB_String_GetLength(const CHNLIB_String *strtag); |
139 | +//----末尾の改行文字を削除 | |
128 | 140 | void CHNLIB_String_DeleteLastCRLF(CHNLIB_String *strtag); |
141 | +//----文字列の比較 | |
129 | 142 | int CHNLIB_String_CompareStringWithCString(const CHNLIB_String *s, const char search[]); |
130 | 143 | int CHNLIB_String_CompareString_Strict(const CHNLIB_String *s, const CHNLIB_String *search); |
144 | +//----文字列の出現回数を取得 | |
131 | 145 | uint CHNLIB_String_GetCountOfContain(const CHNLIB_String *s, const CHNLIB_String *search); |
132 | -//-- | |
146 | +//----文字列のコピーを作成 | |
147 | +CHNLIB_String *CHNLIB_String_Copy(const CHNLIB_String *s); | |
148 | +//----低レベルCString関数 | |
133 | 149 | int CHNLIB_CString_GetByteLength(const char s[]); |
134 | 150 | void CHNLIB_CString_DeleteLastCRLF(char s[]); |
135 | 151 | char *CHNLIB_CString_ExtractByLength(const char s[], int start, int len); |
136 | 152 | int CHNLIB_CString_CompareString(const char s[], const char search[]); |
137 | 153 | int CHNLIB_CString_CompareString_Strict(const char s[], const char search[]); |
138 | 154 | int CHNLIB_CString_CompareString_LeftHand(const char s[], const char search[]); |
155 | +char *CHNLIB_CString_Copy(const char s[]); | |
156 | +char *CHNLIB_CString_Concatenate(const char s1[], const char s2[]); | |
139 | 157 | //@chnlib02.c |
140 | 158 | int CHNLIB_String_Search_UIPArrayStringLocation(const CHNLIB_String *s, int s_start, const CHNLIB_UIPArray *list, int *location); |
141 | 159 | int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, const CHNLIB_UIPArray *list, const CHNLIB_String *s); |
@@ -26,7 +26,9 @@ | ||
26 | 26 | //Declare internal functions |
27 | 27 | // |
28 | 28 | CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void); |
29 | +const CHNLIB_UIPArray **CHNLIB_UIPArray_Internal_CheckIndex(const CHNLIB_UIPArray **array, int *index); | |
29 | 30 | void CHNLIB_UIPArray_Internal_Destruct(void **structure); |
31 | +uint CHNLIB_UIPArray_Internal_GetHash(const void *structure); | |
30 | 32 | |
31 | 33 | // |
32 | 34 | //Define types |
@@ -101,28 +103,18 @@ int CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(CHNLIB_UIPArray **array, u | ||
101 | 103 | |
102 | 104 | int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index) |
103 | 105 | { |
104 | - //戻り値:削除が成功したか(0:成功,-1:指定された添字の要素は存在しない) | |
106 | + //戻り値:関数が失敗したか(False:成功,True:指定された添字の要素は存在しない) | |
105 | 107 | //indexが指し示す要素を削除し、後続の要素は前へつめる。 |
106 | 108 | //削除された要素のpointerの参照先の所有を放棄する(Release)。 |
107 | 109 | int i; |
108 | 110 | |
109 | - for(; (index / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; index -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){ | |
110 | - if(*array == NULL){ | |
111 | - return -1; | |
112 | - } | |
113 | - array = &(*array)->next; | |
114 | - } | |
115 | - if(*array == NULL){ | |
116 | - //大雑把に見て、配列の範囲外 | |
117 | - return -1; | |
118 | - } | |
119 | - if(index >= (*array)->using_tags){ | |
120 | - //詳細に見て、一つのパックの中で、配列の範囲外 | |
121 | - return -1; | |
111 | + array = (CHNLIB_UIPArray **)CHNLIB_UIPArray_Internal_CheckIndex((const CHNLIB_UIPArray **)array, &index); | |
112 | + if(index == CHNLIB_UIPArray_INDEX_NOTFOUND){ | |
113 | + return True; | |
122 | 114 | } |
123 | 115 | |
124 | 116 | //参照先の所有を放棄 |
125 | - CHNLIB_Release(&(*array)->tag[index].pointer); | |
117 | + release((*array)->tag[index].pointer); | |
126 | 118 | |
127 | 119 | for(i = index; i < (*array)->using_tags - 1; i++){ |
128 | 120 | (*array)->tag[i] = (*array)->tag[i + 1]; |
@@ -150,7 +142,7 @@ int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index) | ||
150 | 142 | break; |
151 | 143 | } |
152 | 144 | } |
153 | - return 0; | |
145 | + return False; | |
154 | 146 | } |
155 | 147 | |
156 | 148 | void CHNLIB_UIPArray_FreeOnlyArray(CHNLIB_UIPArray **array) |
@@ -238,14 +230,10 @@ int CHNLIB_UIPArray_GetNumberOfDatas(const CHNLIB_UIPArray *array) | ||
238 | 230 | |
239 | 231 | uint CHNLIB_UIPArray_GetData32ByIndex(const CHNLIB_UIPArray *array, int index) |
240 | 232 | { |
241 | - //retv:array[index]->data32 | |
242 | - for(; (index / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; index -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){ | |
243 | - if(array == NULL){ | |
244 | - return 0; | |
245 | - } | |
246 | - array = array->next; | |
247 | - } | |
248 | - if(array == NULL){ | |
233 | + //index番目のdata32を返す。 | |
234 | + //無効なindex及びArrayを指定した場合は、0を返す。 | |
235 | + array = *CHNLIB_UIPArray_Internal_CheckIndex(&array, &index); | |
236 | + if(index == CHNLIB_UIPArray_INDEX_NOTFOUND){ | |
249 | 237 | return 0; |
250 | 238 | } |
251 | 239 | return array->tag[index].data32; |
@@ -253,19 +241,14 @@ uint CHNLIB_UIPArray_GetData32ByIndex(const CHNLIB_UIPArray *array, int index) | ||
253 | 241 | |
254 | 242 | uint CHNLIB_UIPArray_SetData32ByIndex(CHNLIB_UIPArray *array, int index, uint data32) |
255 | 243 | { |
256 | - //retv = old data32 | |
257 | - | |
244 | + //変更前のdata32が返される。 | |
258 | 245 | uint olddata; |
259 | - | |
260 | - for(; (index / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; index -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){ | |
261 | - if(array == NULL){ | |
262 | - return 0; | |
263 | - } | |
264 | - array = array->next; | |
265 | - } | |
266 | - if(array == NULL){ | |
246 | + | |
247 | + array = (CHNLIB_UIPArray *)*CHNLIB_UIPArray_Internal_CheckIndex((const CHNLIB_UIPArray **)&array, &index); | |
248 | + if(index == CHNLIB_UIPArray_INDEX_NOTFOUND){ | |
267 | 249 | return 0; |
268 | 250 | } |
251 | + | |
269 | 252 | olddata = array->tag[index].data32; |
270 | 253 | array->tag[index].data32 = data32; |
271 | 254 | return olddata; |
@@ -274,15 +257,12 @@ uint CHNLIB_UIPArray_SetData32ByIndex(CHNLIB_UIPArray *array, int index, uint da | ||
274 | 257 | void *CHNLIB_UIPArray_GetPointerByIndex(const CHNLIB_UIPArray *array, int index) |
275 | 258 | { |
276 | 259 | //retv:array[index]->pointer |
277 | - for(; (index / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; index -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){ | |
278 | - if(array == NULL){ | |
279 | - return NULL; | |
280 | - } | |
281 | - array = array->next; | |
282 | - } | |
283 | - if(array == NULL){ | |
260 | + | |
261 | + array = *CHNLIB_UIPArray_Internal_CheckIndex(&array, &index); | |
262 | + if(index == CHNLIB_UIPArray_INDEX_NOTFOUND){ | |
284 | 263 | return NULL; |
285 | 264 | } |
265 | + | |
286 | 266 | return array->tag[index].pointer; |
287 | 267 | } |
288 | 268 |
@@ -378,11 +358,45 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void) | ||
378 | 358 | |
379 | 359 | CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_UIPArray); |
380 | 360 | tag->header.destructor = &CHNLIB_UIPArray_Internal_Destruct; |
361 | + tag->header.getHash = &CHNLIB_UIPArray_Internal_GetHash; | |
381 | 362 | |
382 | 363 | return tag; |
383 | 364 | |
384 | 365 | } |
385 | 366 | |
367 | +const CHNLIB_UIPArray **CHNLIB_UIPArray_Internal_CheckIndex(const CHNLIB_UIPArray **array, int *index) | |
368 | +{ | |
369 | + //*indexの指す要素の含まれるパックを指すポインタへのアドレスを返し、*indexにはそのパック内での添字を格納する。 | |
370 | + //*indexが配列の範囲外だった場合、*indexにCHNLIB_UIPArray_INDEX_NOTFOUNDを格納する。 | |
371 | + //内部関数なので、arrayおよびindexがNULLでないことを仮定する。 | |
372 | + | |
373 | + if((*index) < 0){ | |
374 | + //添字がマイナスの要素は存在しない | |
375 | + (*index) = CHNLIB_UIPArray_INDEX_NOTFOUND; | |
376 | + return array; | |
377 | + } | |
378 | + if(*array == NULL){ | |
379 | + //次のPackが存在しない | |
380 | + (*index) = CHNLIB_UIPArray_INDEX_NOTFOUND; | |
381 | + return array; | |
382 | + } | |
383 | + for(; ((*index) / CHNLIB_UIPArray_INTERNAL_PACKDATAS) > 0; (*index) -= CHNLIB_UIPArray_INTERNAL_PACKDATAS){ | |
384 | + array = (const CHNLIB_UIPArray **)&(*array)->next; | |
385 | + if(*array == NULL){ | |
386 | + //次のPackが存在しない | |
387 | + (*index) = CHNLIB_UIPArray_INDEX_NOTFOUND; | |
388 | + return array; | |
389 | + } | |
390 | + } | |
391 | + | |
392 | + if((*index) >= (*array)->using_tags){ | |
393 | + //詳細に見て、一つのパックの中で、配列の範囲外 | |
394 | + (*index) = CHNLIB_UIPArray_INDEX_NOTFOUND; | |
395 | + return array; | |
396 | + } | |
397 | + return array; | |
398 | +} | |
399 | + | |
386 | 400 | void CHNLIB_UIPArray_Internal_Destruct(void **structure) |
387 | 401 | { |
388 | 402 | //デストラクタ(実際にRelease->freeされる時に呼ばれる) |
@@ -416,4 +430,24 @@ void CHNLIB_UIPArray_Internal_Destruct(void **structure) | ||
416 | 430 | return; |
417 | 431 | } |
418 | 432 | |
433 | +uint CHNLIB_UIPArray_Internal_GetHash(const void *structure) | |
434 | +{ | |
435 | + const CHNLIB_UIPArray *array; | |
436 | + int i, i_max; | |
437 | + uint hash; | |
438 | + | |
439 | + if(CHNLIB_StructureHeader_GetTypeID(structure) != CHNLIB_STRUCT_ID_UIPArray){ | |
440 | + return 0; | |
441 | + } | |
442 | + | |
443 | + hash = 0; | |
444 | + array = (const CHNLIB_UIPArray *)structure; | |
445 | + i_max = CHNLIB_UIPArray_GetNumberOfDatas(array); | |
446 | + for(i = 0; i < i_max; i++){ | |
447 | + hash += CHNLIB_StructureHeader_GetHash(CHNLIB_UIPArray_GetPointerByIndex(array, i)); | |
448 | + } | |
449 | + | |
450 | + return hash; | |
451 | +} | |
452 | + | |
419 | 453 |
@@ -33,6 +33,7 @@ char *CHNLIB_String_Intenal_NullCString = ""; | ||
33 | 33 | |
34 | 34 | CHNLIB_String *CHNLIB_String_Internal_Allocate(void); |
35 | 35 | void CHNLIB_String_Internal_Destruct(void **structure); |
36 | +uint CHNLIB_String_Internal_GetHash(const void *structure); | |
36 | 37 | |
37 | 38 | // |
38 | 39 | //Define types |
@@ -56,6 +57,7 @@ CHNLIB_String *CHNLIB_String_Initialize(const char str[]) | ||
56 | 57 | |
57 | 58 | strtag = CHNLIB_String_Internal_Allocate(); |
58 | 59 | strtag->header.destructor = &CHNLIB_String_Internal_Destruct; |
60 | + strtag->header.getHash = &CHNLIB_String_Internal_GetHash; | |
59 | 61 | |
60 | 62 | if(str != NULL){ |
61 | 63 | CHNLIB_String_SetStringFromCString(strtag, str); |
@@ -65,21 +67,22 @@ CHNLIB_String *CHNLIB_String_Initialize(const char str[]) | ||
65 | 67 | |
66 | 68 | CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...) |
67 | 69 | { |
68 | - CHNLIB_String *strtag; | |
70 | + //CHNLIB_String *strtag; | |
69 | 71 | va_list ap; |
70 | 72 | char str[CHNLIB_MAX_STRING_LENGTH]; |
71 | 73 | |
72 | 74 | va_start(ap, format); |
73 | 75 | vsnprintf(str, sizeof(str), format, ap); |
74 | 76 | va_end(ap); |
75 | - | |
77 | + /* | |
76 | 78 | strtag = CHNLIB_String_Internal_Allocate(); |
77 | 79 | strtag->header.destructor = &CHNLIB_String_Internal_Destruct; |
78 | 80 | |
79 | 81 | if(str != NULL){ |
80 | 82 | CHNLIB_String_SetStringFromCString(strtag, str); |
81 | 83 | } |
82 | - return strtag; | |
84 | + */ | |
85 | + return CHNLIB_String_Initialize(str); | |
83 | 86 | } |
84 | 87 | |
85 | 88 | void CHNLIB_String_Free(CHNLIB_String *strtag) |
@@ -162,6 +165,10 @@ CHNLIB_String *CHNLIB_String_ExtractByLength(const CHNLIB_String *strtag, int st | ||
162 | 165 | char *retstr; |
163 | 166 | CHNLIB_String *retstrtag; |
164 | 167 | |
168 | + if(strtag == NULL){ | |
169 | + return NULL; | |
170 | + } | |
171 | + | |
165 | 172 | retstr = CHNLIB_CString_ExtractByLength(strtag->str, start, len); |
166 | 173 | |
167 | 174 | if(retstr == NULL){ |
@@ -174,6 +181,36 @@ CHNLIB_String *CHNLIB_String_ExtractByLength(const CHNLIB_String *strtag, int st | ||
174 | 181 | return retstrtag; |
175 | 182 | } |
176 | 183 | |
184 | +CHNLIB_String *CHNLIB_String_Concatenate(const CHNLIB_String *s1, const CHNLIB_String *s2) | |
185 | +{ | |
186 | + //二つの文字列を0x00以外のバイトは全て文字と見なして連結する。 | |
187 | + //戻り値はs1,s2の順に結合された、新たな文字列へのポインタ。 | |
188 | + //どちらもNULLの場合はNULLポインタを、 | |
189 | + //どちらかが文字列の場合はその文字列のコピーを新たに作成して返す。 | |
190 | + char *retstr; | |
191 | + CHNLIB_String *retstrtag; | |
192 | + | |
193 | + if(s1 == NULL || s2 == NULL){ | |
194 | + if(s1 != NULL){ | |
195 | + return CHNLIB_String_Copy(s1); | |
196 | + } | |
197 | + if(s2 != NULL){ | |
198 | + return CHNLIB_String_Copy(s2); | |
199 | + } | |
200 | + } | |
201 | + | |
202 | + retstr = CHNLIB_CString_Concatenate(s1->str, s2->str); | |
203 | + | |
204 | + if(retstr == NULL){ | |
205 | + return NULL; | |
206 | + } | |
207 | + | |
208 | + retstrtag = CHNLIB_String_Initialize(NULL); | |
209 | + retstrtag->str = retstr; | |
210 | + | |
211 | + return retstrtag; | |
212 | +} | |
213 | + | |
177 | 214 | int CHNLIB_String_GetLength(const CHNLIB_String *strtag) |
178 | 215 | { |
179 | 216 | if(CHNLIB_StructureHeader_GetTypeID(strtag) != CHNLIB_STRUCT_ID_String){ |
@@ -209,6 +246,27 @@ uint CHNLIB_String_GetCountOfContain(const CHNLIB_String *s, const CHNLIB_String | ||
209 | 246 | return CHNLIB_UTF8_GetCountOfContain(CHNLIB_String_GetReferencePointerOfCString(s), CHNLIB_String_GetReferencePointerOfCString(search)); |
210 | 247 | } |
211 | 248 | |
249 | +CHNLIB_String *CHNLIB_String_Copy(const CHNLIB_String *s) | |
250 | +{ | |
251 | + char *retstr; | |
252 | + CHNLIB_String *retstrtag; | |
253 | + | |
254 | + if(s == NULL){ | |
255 | + return NULL; | |
256 | + } | |
257 | + | |
258 | + retstr = CHNLIB_CString_Copy(s->str); | |
259 | + | |
260 | + if(retstr == NULL){ | |
261 | + return NULL; | |
262 | + } | |
263 | + | |
264 | + retstrtag = CHNLIB_String_Initialize(NULL); | |
265 | + retstrtag->str = retstr; | |
266 | + | |
267 | + return retstrtag; | |
268 | +} | |
269 | + | |
212 | 270 | // |
213 | 271 | //Functions(CString(char[])) |
214 | 272 | // |
@@ -310,6 +368,7 @@ int CHNLIB_CString_CompareString(const char s[], const char search[]) | ||
310 | 368 | //終端文字'\0'はカウントしない。 |
311 | 369 | //search[]に含まれる文字(終端文字除く)がすべて入っていれば一致とみなす。 |
312 | 370 | //どちらかがNULLであった場合は、Falseを返す。 |
371 | + //一致文字数が0(search=="")だった場合もFalseを返す。 | |
313 | 372 | int i; |
314 | 373 | |
315 | 374 | if(s == NULL || search == NULL){ |
@@ -324,6 +383,9 @@ int CHNLIB_CString_CompareString(const char s[], const char search[]) | ||
324 | 383 | return False; |
325 | 384 | } |
326 | 385 | } |
386 | + if(i == 0){ | |
387 | + return False; | |
388 | + } | |
327 | 389 | return True; |
328 | 390 | } |
329 | 391 |
@@ -368,6 +430,63 @@ int CHNLIB_CString_CompareString_LeftHand(const char s[], const char search[]) | ||
368 | 430 | |
369 | 431 | return i; |
370 | 432 | } |
433 | + | |
434 | +char *CHNLIB_CString_Copy(const char s[]) | |
435 | +{ | |
436 | + //与えられた文字列と等価な文字列を作成して、そのポインタを返す。 | |
437 | + //有効な文字列が無い場合(NULLポインタも含む)、NULLを返す。 | |
438 | + char *copyString; | |
439 | + int i, i_max; | |
440 | + | |
441 | + i_max = CHNLIB_CString_GetByteLength(s) + 1; | |
442 | + if(i_max <= 1){ | |
443 | + return NULL; | |
444 | + } | |
445 | + | |
446 | + copyString = CHNLIB_System_AllocateMemory_Strict(i_max, CHNLIB_DEBUG_ARGUMENTS); | |
447 | + for(i = 0; i < i_max - 1; i++){ | |
448 | + copyString[i] = s[i]; | |
449 | + } | |
450 | + copyString[i] = '\0'; | |
451 | + | |
452 | + return copyString; | |
453 | +} | |
454 | + | |
455 | +char *CHNLIB_CString_Concatenate(const char s1[], const char s2[]) | |
456 | +{ | |
457 | + //二つの文字列を0x00以外のバイトは全て文字と見なして連結する。 | |
458 | + //戻り値はs1,s2の順に結合された、新たな文字列へのポインタ。 | |
459 | + //どちらもNULLの場合はNULLポインタを、 | |
460 | + //どちらかが文字列の場合はその文字列のコピーを新たに作成して返す。 | |
461 | + int s1_len, s2_len, i, concatenatedSize; | |
462 | + char *concatenatedString; | |
463 | + | |
464 | + if(s1 == NULL || s2 == NULL){ | |
465 | + if(s1 != NULL){ | |
466 | + return CHNLIB_CString_Copy(s1); | |
467 | + } | |
468 | + | |
469 | + if(s2 != NULL){ | |
470 | + return CHNLIB_CString_Copy(s2); | |
471 | + } | |
472 | + } | |
473 | + | |
474 | + s1_len = CHNLIB_CString_GetByteLength(s1); | |
475 | + s2_len = CHNLIB_CString_GetByteLength(s2); | |
476 | + | |
477 | + concatenatedSize = s1_len + s2_len + 1; | |
478 | + | |
479 | + concatenatedString = CHNLIB_System_AllocateMemory_Strict(concatenatedSize, CHNLIB_DEBUG_ARGUMENTS); | |
480 | + for(i = 0; i < s1_len; i++){ | |
481 | + concatenatedString[i] = s1[i]; | |
482 | + } | |
483 | + for(i = 0; i < s2_len; i++){ | |
484 | + concatenatedString[i + s1_len] = s2[i]; | |
485 | + } | |
486 | + concatenatedString[concatenatedSize - 1] = '\0'; | |
487 | + | |
488 | + return concatenatedString; | |
489 | +} | |
371 | 490 | |
372 | 491 | // |
373 | 492 | //Internal functions |
@@ -401,3 +520,22 @@ void CHNLIB_String_Internal_Destruct(void **structure) | ||
401 | 520 | |
402 | 521 | return; |
403 | 522 | } |
523 | + | |
524 | +uint CHNLIB_String_Internal_GetHash(const void *structure) | |
525 | +{ | |
526 | + uint hash; | |
527 | + const char *p; | |
528 | + | |
529 | + hash = 0; | |
530 | + | |
531 | + if(CHNLIB_StructureHeader_GetTypeID(structure) == CHNLIB_STRUCT_ID_String){ | |
532 | + p = ((CHNLIB_String *)structure)->str; | |
533 | + if(p != NULL){ | |
534 | + for(; *p != '\0'; p++){ | |
535 | + hash += *(unsigned char *)p; | |
536 | + } | |
537 | + } | |
538 | + } | |
539 | + | |
540 | + return hash; | |
541 | +} |
@@ -11,25 +11,29 @@ | ||
11 | 11 | |
12 | 12 | int main(int argc, const char * argv[]) |
13 | 13 | { |
14 | - //CHNLIB_String *s, *t; | |
15 | - CHNLIB_UIPArray *separated; | |
16 | - char str[1024]; | |
17 | - int i; | |
14 | + CHNLIB_String *str1, *str2, *str; | |
15 | + CHNLIB_UIPArray *array; | |
18 | 16 | |
19 | - separated = CHNLIB_UIPArray_Initialize(); | |
17 | + str = CHNLIB_String_Initialize("あいうえおかきs"); | |
18 | + printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); | |
20 | 19 | |
21 | - for(i = 0; i < 32; i++){ | |
22 | - snprintf(str, sizeof(str), "Number %d.", i); | |
23 | - CHNLIB_UIPArray_AppendLast(&separated, i, autorelease(CHNLIB_String_Initialize(str))); | |
24 | - } | |
25 | - | |
26 | - CHNLIB_Debug_PrintStructureData(separated, 0); | |
20 | + str1 = CHNLIB_String_Initialize("あい"); | |
21 | + printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str1), CHNLIB_String_GetReferencePointerOfCString(str1)); | |
22 | + str2 = CHNLIB_String_Initialize("かき"); | |
23 | + printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str2), CHNLIB_String_GetReferencePointerOfCString(str2)); | |
24 | + | |
25 | + str = CHNLIB_String_Concatenate(str1, str2); | |
26 | + printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); | |
27 | 27 | |
28 | - //CHNLIB_Release((void **)&separated); | |
29 | - release(separated); | |
28 | + str = CHNLIB_String_Concatenate(str, str); | |
29 | + printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); | |
30 | 30 | |
31 | + array = CHNLIB_UIPArray_Initialize(); | |
32 | + CHNLIB_UIPArray_AppendLast(&array, 0, str); | |
33 | + printf("0x%X", CHNLIB_StructureHeader_GetHash(array)); | |
31 | 34 | |
32 | - CHNLIB_Debug_PrintStructureData(separated, 0); | |
35 | + CHNLIB_UIPArray_AppendLast(&array, 0, str); | |
36 | + printf("0x%X", CHNLIB_StructureHeader_GetHash(array)); | |
33 | 37 | |
34 | 38 | return 0; |
35 | 39 | } |