BlitzMax Extended  0.8.9
Pushing Blitz to the Max.
blitz_string.h
Go to the documentation of this file.
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 BBInt bb_string_buffer_findall(const BBChar * restrict str_buf,
00154                                const BBChar * restrict sub_buf,
00155                                const BBSize str_len,
00156                                const BBSize sub_len,
00157                                BBInt * const matches) __attribute__ ((pure));
00158 
00159 #endif  // BB_ENABLE_HELPER
00160 
00161 
00162 
00163 //  Built-in
00164 
00174 BBString* bbStringToString(const BBString *str);
00175 
00192 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure));
00193 
00207 BBString* bbStringClone(const BBString *str);
00208 
00209 
00210 
00211 //  API
00212 
00225 BBString* bbStringNew(BBInt len);
00226 
00234 BBString* bbStringFromChar(BBInt codepoint);
00235 
00243 BBString* bbStringFromInt(BBInt number);
00244 
00250 BBString* bbStringFromLong(BBLong number);
00251 
00257 BBString* bbStringFromFloat(BBFloat number);
00258 
00264 BBString* bbStringFromDouble(BBDouble number);
00265 
00279 BBString* bbStringFromBytes(const char *buf, BBInt size);
00280 
00294 BBString* bbStringFromShorts(const BBShort *buf, BBInt size);
00295 
00313 BBString* bbStringFromInts(const BBInt *buf, BBInt size);
00314 
00327 BBString* bbStringFromArray(const BBArray *arr);
00328 
00336 BBString* bbStringFromCString(const char *c_str);
00337 
00345 BBString* bbStringFromWString(const BBChar *w_str);
00346 
00356 BBString* bbStringFromUTF8String(const char *utf8_str);
00357 
00358 
00366 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
00367 
00374 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
00375 
00388 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure));
00389 
00390 
00391 
00402 BBString* bbStringConcat(const BBString *str, const BBString *rhs);
00403 
00404 
00412 enum BBStringSide
00413 {
00415     BBSTRING_SIDE_BOTH,
00416     
00418     BBSTRING_SIDE_LEFT,
00419     
00421     BBSTRING_SIDE_RIGHT
00422 };
00423 
00424 typedef enum BBStringSide   BBStringSide;
00425 
00426 #define BBSTRING_SIDE_ISVALID(side) \
00427     ((side) >= BBSTRING_SIDE_BOTH && (side) <= BBSTRING_SIDE_RIGHT)
00428 
00441 BBString* bbStringTrim(const BBString *str, BBStringSide side);
00442 
00454 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
00455 
00468 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
00469 
00475 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
00476 
00477 
00478 
00483 #define BBSTRING_NOTFOUND   (-1)
00484 
00501 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
00502 
00517 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
00518 
00536 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
00537 
00551 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
00552 
00560 BBString* bbStringToLower(const BBString *str);
00561 
00569 BBString* bbStringToUpper(const BBString *str);
00570 
00576 BBInt bbStringToInt(const BBString *str);
00577 
00583 BBFloat bbStringToFloat(const BBString *str);
00584 
00590 BBDouble bbStringToDouble(const BBString *str);
00591 
00598 void bbStringToLong(const BBString *str, BBLong *result);
00599 
00611 char* bbStringToCString(const BBString *str);
00612 
00621 BBChar* bbStringToWString(const BBString *str);
00622 
00634 char* bbStringToUTF8String(const BBString *str);
00635 
00646 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
00647 
00651 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
00652 
00653 
00654 
00655 //  Additions by BME
00656 
00670 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
00671 
00680 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
00681 
00689 BBString* bbStringReverse(const BBString *str);
00690 
00708 BBString* bbStringTimes(const BBString *str, BBInt count);
00709 
00733 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
00734 
00741 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
00742 
00749 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
00750 
00759 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
00760 
00761 //  intern
00762 
00763 char* bbTmpCString(const BBString *str);
00764 BBChar* bbTmpWString(const BBString *str);
00765 char* bbTmpUTF8String(const BBString *str);
00766 
00767 BB_END_DECLS
00768 
00769 #endif  // BLITZ_STRING_H