![]() |
BlitzMax Extended
0.8.11
Pushing Blitz to the Max.
|
00001 00009 #ifndef BLITZ_STRING_H 00010 #define BLITZ_STRING_H 00011 00012 #include "_common.h" 00013 00014 #include "blitz_array.h" 00015 #include "blitz_object.h" 00016 00017 BB_BEGIN_DECLS 00018 00019 // Used internally to calculate the amount of memory necessary 00020 // to allocate a string with a certain length. 00021 #define BBSTRING_SIZE_CALC(len) (sizeof (BBString) + (len) * sizeof (BBChar)) 00022 00031 #define BBSTRING_SIZE(str) BBSTRING_SIZE_CALC((str)->length) 00032 00037 #define BBSTRING_MAX_LENGTH ((BBSize)(BBSIZE_MAX / sizeof (BBChar)) - sizeof (BBString)) 00038 //#define BBSTRING_MAX_LENGTH 65535 00039 00057 #define BBSTRING_ALLOC(len) \ 00058 ({ \ 00059 (BBString *) \ 00060 bbGCAllocObject( \ 00061 BBSTRING_SIZE_CALC(len), \ 00062 &bbStringClass, \ 00063 BBGC_ATOMIC \ 00064 ); \ 00065 }) 00066 00067 /* Creates a string from a BBChar buffer 00068 * 00069 * \param buffer A buffer containing UCS2 encoded string data 00070 * \param len The length of the buffer and the new string 00071 * \returns A pointer to a newly allocated string with the given contents 00072 */ 00073 #define BBSTRING_FROMBUFFER(buffer, len) \ 00074 ({ \ 00075 BB_ASSERT((len) > 0 && (len) < BBSTRING_MAX_LENGTH, "Invalid buffer length"); \ 00076 BBString *_str = BBSTRING_ALLOC((len)); \ 00077 _str->length = (len); \ 00078 memcpy(_str->buf, (buffer), (len) * sizeof (BBChar)); \ 00079 _str; \ 00080 }) 00081 00099 struct BBString 00100 { 00106 BBClass* clas; 00107 int refs; 00108 00116 BBSize length; 00117 00119 BBChar buf[]; 00120 }; 00121 00127 extern BBClass bbStringClass; 00128 00137 extern BBString bbEmptyString; 00138 00145 #define BBNULLSTRING (&bbEmptyString) 00146 00147 #ifdef BB_ENABLE_HELPER 00148 00149 bool bb_string_buffer_equals(const BBChar * restrict str_buf, 00150 const BBChar * restrict cmp_buf, 00151 BBSize len) __attribute__ ((pure)); 00152 00153 #endif // BB_ENABLE_HELPER 00154 00155 00156 00157 // Built-in 00158 00168 BBString* bbStringToString(const BBString *str); 00169 00186 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure)); 00187 00201 BBString* bbStringClone(const BBString *str); 00202 00203 00204 00205 // API 00206 00219 BBString* bbStringNew(BBInt len); 00220 00228 BBString* bbStringFromChar(BBInt codepoint); 00229 00237 BBString* bbStringFromInt(BBInt number); 00238 00244 BBString* bbStringFromLong(BBLong number); 00245 00251 BBString* bbStringFromFloat(BBFloat number); 00252 00258 BBString* bbStringFromDouble(BBDouble number); 00259 00273 BBString* bbStringFromBytes(const char *buf, BBInt size); 00274 00288 BBString* bbStringFromShorts(const BBShort *buf, BBInt size); 00289 00307 BBString* bbStringFromInts(const BBInt *buf, BBInt size); 00308 00321 BBString* bbStringFromArray(const BBArray *arr); 00322 00330 BBString* bbStringFromCString(const char *c_str); 00331 00339 BBString* bbStringFromWString(const BBChar *w_str); 00340 00350 BBString* bbStringFromUTF8String(const char *utf8_str); 00351 00352 00360 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure)); 00361 00368 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure)); 00369 00382 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure)); 00383 00384 00385 00396 BBString* bbStringConcat(const BBString *str, const BBString *rhs); 00397 00398 00406 enum BBStringSide 00407 { 00409 BBSTRING_SIDE_BOTH, 00410 00412 BBSTRING_SIDE_LEFT, 00413 00415 BBSTRING_SIDE_RIGHT 00416 }; 00417 00418 typedef enum BBStringSide BBStringSide; 00419 00420 #define BBSTRING_SIDE_ISVALID(side) \ 00421 ((side) >= BBSTRING_SIDE_BOTH && (side) <= BBSTRING_SIDE_RIGHT) 00422 00435 BBString* bbStringTrim(const BBString *str, BBStringSide side); 00436 00448 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end); 00449 00462 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement); 00463 00469 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure)); 00470 00471 00472 00477 #define BBSTRING_NOTFOUND (-1) 00478 00495 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure)); 00496 00511 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure)); 00512 00530 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index); 00531 00545 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure)); 00546 00554 BBString* bbStringToLower(const BBString *str); 00555 00563 BBString* bbStringToUpper(const BBString *str); 00564 00570 BBInt bbStringToInt(const BBString *str); 00571 00577 BBFloat bbStringToFloat(const BBString *str); 00578 00584 BBDouble bbStringToDouble(const BBString *str); 00585 00592 void bbStringToLong(const BBString *str, BBLong *result); 00593 00605 char* bbStringToCString(const BBString *str); 00606 00615 BBChar* bbStringToWString(const BBString *str); 00616 00628 char* bbStringToUTF8String(const BBString *str); 00629 00640 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator); 00641 00645 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits); 00646 00647 00648 00649 // Additions by BME 00650 00664 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure)); 00665 00674 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure)); 00675 00683 BBString* bbStringReverse(const BBString *str); 00684 00702 BBString* bbStringTimes(const BBString *str, BBInt count); 00703 00727 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len); 00728 00735 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure)); 00736 00743 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure)); 00744 00753 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure)); 00754 00755 // intern 00756 00757 char* bbTmpCString(const BBString *str); 00758 BBChar* bbTmpWString(const BBString *str); 00759 char* bbTmpUTF8String(const BBString *str); 00760 00761 BB_END_DECLS 00762 00763 #endif // BLITZ_STRING_H