• R/O
  • HTTP
  • SSH
  • HTTPS

CHNOSProject: Commit

CHNOSProjectのすべてのプロジェクトのソースのルート


Commit MetaInfo

Revisión1ba1ff02213d50410430f7c01c6ec362ad3d8aca (tree)
Tiempo2013-06-29 00:57:45
Autorhikarupsp <hikarupsp@user...>
Commiterhikarupsp

Log Message

chnlib:構造体のハッシュ値取得を実装。検索の高速化に利用する予定。文字列の連結・コピーも実装。合わせてlibtestも更新。

Cambiar Resumen

Diferencia incremental

--- a/CHNOSProject/CHNOSProject.xcodeproj/project.pbxproj
+++ b/CHNOSProject/CHNOSProject.xcodeproj/project.pbxproj
@@ -7,13 +7,6 @@
77 objects = {
88
99 /* Begin PBXContainerItemProxy section */
10- 177F343E17339E2F004D6778 /* PBXContainerItemProxy */ = {
11- isa = PBXContainerItemProxy;
12- containerPortal = 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */;
13- proxyType = 2;
14- remoteGlobalIDString = 177F33E3172A4D98004D6778;
15- remoteInfo = chnl;
16- };
1710 1783027F1719927500FF22B2 /* PBXContainerItemProxy */ = {
1811 isa = PBXContainerItemProxy;
1912 containerPortal = 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */;
@@ -59,7 +52,7 @@
5952 /* End PBXContainerItemProxy section */
6053
6154 /* 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>"; };
6356 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SimpleHTMLEditor.xcodeproj; path = SimpleHTMLEditor/SimpleHTMLEditor.xcodeproj; sourceTree = "<group>"; };
6457 17D0F3BD16DA2F3700C62B79 /* AI002.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AI002.xcodeproj; path = AI002/AI002.xcodeproj; sourceTree = "<group>"; };
6558 17D0F3C316DA2F3C00C62B79 /* AI003.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = AI003.xcodeproj; path = AI003/AI003.xcodeproj; sourceTree = "<group>"; };
@@ -72,7 +65,7 @@
7265 1724D6D416DA113E008A8FE2 = {
7366 isa = PBXGroup;
7467 children = (
75- 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */,
68+ 17677F96176B838900F12210 /* chnos.xcodeproj */,
7669 178302761719927300FF22B2 /* SimpleHTMLEditor.xcodeproj */,
7770 17EE90AA16DB67DB005238B2 /* libtest.xcodeproj */,
7871 17D0F42816DA37C400C62B79 /* c2wiki.xcodeproj */,
@@ -82,10 +75,9 @@
8275 );
8376 sourceTree = "<group>";
8477 };
85- 177F33DA172A4B05004D6778 /* Products */ = {
78+ 17677F97176B838900F12210 /* Products */ = {
8679 isa = PBXGroup;
8780 children = (
88- 177F343F17339E2F004D6778 /* chnl */,
8981 );
9082 name = Products;
9183 sourceTree = "<group>";
@@ -173,8 +165,8 @@
173165 ProjectRef = 17D0F3F416DA305000C62B79 /* chn.xcodeproj */;
174166 },
175167 {
176- ProductGroup = 177F33DA172A4B05004D6778 /* Products */;
177- ProjectRef = 177F33D9172A4B05004D6778 /* ctoolset.xcodeproj */;
168+ ProductGroup = 17677F97176B838900F12210 /* Products */;
169+ ProjectRef = 17677F96176B838900F12210 /* chnos.xcodeproj */;
178170 },
179171 {
180172 ProductGroup = 17EE90AB16DB67DB005238B2 /* Products */;
@@ -192,13 +184,6 @@
192184 /* End PBXProject section */
193185
194186 /* 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- };
202187 178302801719927500FF22B2 /* SimpleHTMLEditor.app */ = {
203188 isa = PBXReferenceProxy;
204189 fileType = wrapper.application;
Binary files a/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate and b/CHNOSProject/CHNOSProject.xcodeproj/project.xcworkspace/xcuserdata/hikaru.xcuserdatad/UserInterfaceState.xcuserstate differ
--- a/CHNOSProject/CHNOSProject.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
+++ b/CHNOSProject/CHNOSProject.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -96,14 +96,12 @@
9696 shouldBeEnabled = "No"
9797 ignoreCount = "0"
9898 continueAfterRunningActions = "No"
99- filePath = "libtest/libtest/main.c"
100- timestampString = "390597364.50073"
99+ filePath = "SimpleHTMLEditor/SimpleHTMLEditor/AppDelegate.h"
100+ timestampString = "394127181.292504"
101101 startingColumnNumber = "9223372036854775807"
102102 endingColumnNumber = "9223372036854775807"
103- startingLineNumber = "28"
104- endingLineNumber = "28"
105- landmarkName = "main()"
106- landmarkType = "7">
103+ startingLineNumber = "8"
104+ endingLineNumber = "8">
107105 </FileBreakpoint>
108106 </FileBreakpoints>
109107 </Bucket>
--- a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
+++ b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -7,7 +7,7 @@
77 <key>chn.xcscheme</key>
88 <dict>
99 <key>orderHint</key>
10- <integer>1</integer>
10+ <integer>2</integer>
1111 </dict>
1212 </dict>
1313 <key>SuppressBuildableAutocreation</key>
--- a/CHNOSProject/chn/chnlib.c
+++ b/CHNOSProject/chn/chnlib.c
@@ -108,6 +108,7 @@ void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint type
108108 #endif
109109 header->referenceCount = 1;
110110 header->destructor = NULL;
111+ header->getHash = NULL;
111112 return;
112113 }
113114
@@ -128,6 +129,24 @@ uint CHNLIB_StructureHeader_GetTypeID(const void *structure)
128129 return CHNLIB_STRUCT_ID_Null;
129130 }
130131
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+
131150 void *CHNLIB_System_AllocateMemory_Strict(int size, const char filename[], int line, const char funcname[])
132151 {
133152 //mallocを行い、確保が失敗した場合にメッセージを出力してプログラムを終了する。
--- a/CHNOSProject/chn/chnlib.h
+++ b/CHNOSProject/chn/chnlib.h
@@ -46,6 +46,7 @@ struct CHNLIB_STRUCTURE_HEADER {
4646 uint typeid;
4747 uint referenceCount;
4848 void (*destructor)(void **structrue);
49+ uint (*getHash)(const void *structure);
4950 };
5051
5152 //
@@ -78,6 +79,7 @@ void CHNLIB_Debug_PrintStructureData(void *structure, uint level);
7879 void CHNLIB_ReportError(const char format[], const char filename[], int line, const char funcname[], ...); //("", CHNLIB_DEBUG_ARGUMENTS);
7980 void CHNLIB_StructureHeader_Initialize(CHNLIB_StructureHeader *header, uint typeid);
8081 uint CHNLIB_StructureHeader_GetTypeID(const void *structure);
82+uint CHNLIB_StructureHeader_GetHash(const void *structure);
8183 void *CHNLIB_System_AllocateMemory_Strict(int size, const char filename[], int line, const char funcname[]);
8284 void CHNLIB_System_FreeMemory(void *p, const char filename[], int line, const char funcname[]);
8385 void CHNLIB_Retain(void **structure);
@@ -117,25 +119,41 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB
117119 //@chnlib01.c
118120 typedef struct CHNLIB_STRING CHNLIB_String;
119121 struct CHNLIB_STRING;
122+//----文字列の初期化
120123 CHNLIB_String *CHNLIB_String_Initialize(const char str[]);
121124 CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...);
125+//----文字列の解放
122126 void CHNLIB_String_Free(CHNLIB_String *strtag);
127+//----文字列の設定
123128 int CHNLIB_String_SetStringFromCString(CHNLIB_String *strtag, const char s[]);
129+//----文字列の表示
124130 int CHNLIB_String_Print(CHNLIB_String *strtag);
131+//----文字列の参照の取得
125132 const char *CHNLIB_String_GetReferencePointerOfCString(const CHNLIB_String *strtag);
133+//----文字列の切り出し
126134 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+//----文字列の長さを取得
127138 int CHNLIB_String_GetLength(const CHNLIB_String *strtag);
139+//----末尾の改行文字を削除
128140 void CHNLIB_String_DeleteLastCRLF(CHNLIB_String *strtag);
141+//----文字列の比較
129142 int CHNLIB_String_CompareStringWithCString(const CHNLIB_String *s, const char search[]);
130143 int CHNLIB_String_CompareString_Strict(const CHNLIB_String *s, const CHNLIB_String *search);
144+//----文字列の出現回数を取得
131145 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関数
133149 int CHNLIB_CString_GetByteLength(const char s[]);
134150 void CHNLIB_CString_DeleteLastCRLF(char s[]);
135151 char *CHNLIB_CString_ExtractByLength(const char s[], int start, int len);
136152 int CHNLIB_CString_CompareString(const char s[], const char search[]);
137153 int CHNLIB_CString_CompareString_Strict(const char s[], const char search[]);
138154 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[]);
139157 //@chnlib02.c
140158 int CHNLIB_String_Search_UIPArrayStringLocation(const CHNLIB_String *s, int s_start, const CHNLIB_UIPArray *list, int *location);
141159 int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, const CHNLIB_UIPArray *list, const CHNLIB_String *s);
--- a/CHNOSProject/chn/chnlib00.c
+++ b/CHNOSProject/chn/chnlib00.c
@@ -26,7 +26,9 @@
2626 //Declare internal functions
2727 //
2828 CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void);
29+const CHNLIB_UIPArray **CHNLIB_UIPArray_Internal_CheckIndex(const CHNLIB_UIPArray **array, int *index);
2930 void CHNLIB_UIPArray_Internal_Destruct(void **structure);
31+uint CHNLIB_UIPArray_Internal_GetHash(const void *structure);
3032
3133 //
3234 //Define types
@@ -101,28 +103,18 @@ int CHNLIB_UIPArray_AppendLast_ProtectFromDuplication(CHNLIB_UIPArray **array, u
101103
102104 int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index)
103105 {
104- //戻り値:削除が成功したか(0:成功,-1:指定された添字の要素は存在しない)
106+ //戻り値:関数が失敗したか(False:成功,True:指定された添字の要素は存在しない)
105107 //indexが指し示す要素を削除し、後続の要素は前へつめる。
106108 //削除された要素のpointerの参照先の所有を放棄する(Release)。
107109 int i;
108110
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;
122114 }
123115
124116 //参照先の所有を放棄
125- CHNLIB_Release(&(*array)->tag[index].pointer);
117+ release((*array)->tag[index].pointer);
126118
127119 for(i = index; i < (*array)->using_tags - 1; i++){
128120 (*array)->tag[i] = (*array)->tag[i + 1];
@@ -150,7 +142,7 @@ int CHNLIB_UIPArray_RemoveByIndex(CHNLIB_UIPArray **array, int index)
150142 break;
151143 }
152144 }
153- return 0;
145+ return False;
154146 }
155147
156148 void CHNLIB_UIPArray_FreeOnlyArray(CHNLIB_UIPArray **array)
@@ -238,14 +230,10 @@ int CHNLIB_UIPArray_GetNumberOfDatas(const CHNLIB_UIPArray *array)
238230
239231 uint CHNLIB_UIPArray_GetData32ByIndex(const CHNLIB_UIPArray *array, int index)
240232 {
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){
249237 return 0;
250238 }
251239 return array->tag[index].data32;
@@ -253,19 +241,14 @@ uint CHNLIB_UIPArray_GetData32ByIndex(const CHNLIB_UIPArray *array, int index)
253241
254242 uint CHNLIB_UIPArray_SetData32ByIndex(CHNLIB_UIPArray *array, int index, uint data32)
255243 {
256- //retv = old data32
257-
244+ //変更前のdata32が返される。
258245 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){
267249 return 0;
268250 }
251+
269252 olddata = array->tag[index].data32;
270253 array->tag[index].data32 = data32;
271254 return olddata;
@@ -274,15 +257,12 @@ uint CHNLIB_UIPArray_SetData32ByIndex(CHNLIB_UIPArray *array, int index, uint da
274257 void *CHNLIB_UIPArray_GetPointerByIndex(const CHNLIB_UIPArray *array, int index)
275258 {
276259 //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){
284263 return NULL;
285264 }
265+
286266 return array->tag[index].pointer;
287267 }
288268
@@ -378,11 +358,45 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_Internal_Allocate(void)
378358
379359 CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_UIPArray);
380360 tag->header.destructor = &CHNLIB_UIPArray_Internal_Destruct;
361+ tag->header.getHash = &CHNLIB_UIPArray_Internal_GetHash;
381362
382363 return tag;
383364
384365 }
385366
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+
386400 void CHNLIB_UIPArray_Internal_Destruct(void **structure)
387401 {
388402 //デストラクタ(実際にRelease->freeされる時に呼ばれる)
@@ -416,4 +430,24 @@ void CHNLIB_UIPArray_Internal_Destruct(void **structure)
416430 return;
417431 }
418432
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+
419453
--- a/CHNOSProject/chn/chnlib01.c
+++ b/CHNOSProject/chn/chnlib01.c
@@ -33,6 +33,7 @@ char *CHNLIB_String_Intenal_NullCString = "";
3333
3434 CHNLIB_String *CHNLIB_String_Internal_Allocate(void);
3535 void CHNLIB_String_Internal_Destruct(void **structure);
36+uint CHNLIB_String_Internal_GetHash(const void *structure);
3637
3738 //
3839 //Define types
@@ -56,6 +57,7 @@ CHNLIB_String *CHNLIB_String_Initialize(const char str[])
5657
5758 strtag = CHNLIB_String_Internal_Allocate();
5859 strtag->header.destructor = &CHNLIB_String_Internal_Destruct;
60+ strtag->header.getHash = &CHNLIB_String_Internal_GetHash;
5961
6062 if(str != NULL){
6163 CHNLIB_String_SetStringFromCString(strtag, str);
@@ -65,21 +67,22 @@ CHNLIB_String *CHNLIB_String_Initialize(const char str[])
6567
6668 CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...)
6769 {
68- CHNLIB_String *strtag;
70+ //CHNLIB_String *strtag;
6971 va_list ap;
7072 char str[CHNLIB_MAX_STRING_LENGTH];
7173
7274 va_start(ap, format);
7375 vsnprintf(str, sizeof(str), format, ap);
7476 va_end(ap);
75-
77+ /*
7678 strtag = CHNLIB_String_Internal_Allocate();
7779 strtag->header.destructor = &CHNLIB_String_Internal_Destruct;
7880
7981 if(str != NULL){
8082 CHNLIB_String_SetStringFromCString(strtag, str);
8183 }
82- return strtag;
84+ */
85+ return CHNLIB_String_Initialize(str);
8386 }
8487
8588 void CHNLIB_String_Free(CHNLIB_String *strtag)
@@ -162,6 +165,10 @@ CHNLIB_String *CHNLIB_String_ExtractByLength(const CHNLIB_String *strtag, int st
162165 char *retstr;
163166 CHNLIB_String *retstrtag;
164167
168+ if(strtag == NULL){
169+ return NULL;
170+ }
171+
165172 retstr = CHNLIB_CString_ExtractByLength(strtag->str, start, len);
166173
167174 if(retstr == NULL){
@@ -174,6 +181,36 @@ CHNLIB_String *CHNLIB_String_ExtractByLength(const CHNLIB_String *strtag, int st
174181 return retstrtag;
175182 }
176183
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+
177214 int CHNLIB_String_GetLength(const CHNLIB_String *strtag)
178215 {
179216 if(CHNLIB_StructureHeader_GetTypeID(strtag) != CHNLIB_STRUCT_ID_String){
@@ -209,6 +246,27 @@ uint CHNLIB_String_GetCountOfContain(const CHNLIB_String *s, const CHNLIB_String
209246 return CHNLIB_UTF8_GetCountOfContain(CHNLIB_String_GetReferencePointerOfCString(s), CHNLIB_String_GetReferencePointerOfCString(search));
210247 }
211248
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+
212270 //
213271 //Functions(CString(char[]))
214272 //
@@ -310,6 +368,7 @@ int CHNLIB_CString_CompareString(const char s[], const char search[])
310368 //終端文字'\0'はカウントしない。
311369 //search[]に含まれる文字(終端文字除く)がすべて入っていれば一致とみなす。
312370 //どちらかがNULLであった場合は、Falseを返す。
371+ //一致文字数が0(search=="")だった場合もFalseを返す。
313372 int i;
314373
315374 if(s == NULL || search == NULL){
@@ -324,6 +383,9 @@ int CHNLIB_CString_CompareString(const char s[], const char search[])
324383 return False;
325384 }
326385 }
386+ if(i == 0){
387+ return False;
388+ }
327389 return True;
328390 }
329391
@@ -368,6 +430,63 @@ int CHNLIB_CString_CompareString_LeftHand(const char s[], const char search[])
368430
369431 return i;
370432 }
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+}
371490
372491 //
373492 //Internal functions
@@ -401,3 +520,22 @@ void CHNLIB_String_Internal_Destruct(void **structure)
401520
402521 return;
403522 }
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+}
--- a/CHNOSProject/libtest/libtest/main.c
+++ b/CHNOSProject/libtest/libtest/main.c
@@ -11,25 +11,29 @@
1111
1212 int main(int argc, const char * argv[])
1313 {
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;
1816
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));
2019
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));
2727
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));
3030
31+ array = CHNLIB_UIPArray_Initialize();
32+ CHNLIB_UIPArray_AppendLast(&array, 0, str);
33+ printf("0x%X", CHNLIB_StructureHeader_GetHash(array));
3134
32- CHNLIB_Debug_PrintStructureData(separated, 0);
35+ CHNLIB_UIPArray_AppendLast(&array, 0, str);
36+ printf("0x%X", CHNLIB_StructureHeader_GetHash(array));
3337
3438 return 0;
3539 }
Show on old repository browser