BlitzMax Extended  0.8.11
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 #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  = 0,
00410     
00412     BBSTRING_SIDE_LEFT  = -1,
00413     
00415     BBSTRING_SIDE_RIGHT = 1
00416 };
00417 
00418 typedef enum BBStringSide   BBStringSide;
00419 
00420 #define BBSTRING_SIDE_ISVALID(side) \
00421     ((side) >= BBSTRING_SIDE_LEFT && (side) <= BBSTRING_SIDE_RIGHT)
00422 
00435 BBString* bbStringTrim(const BBString *str, BBStringSide side);
00436 
00451 BBString* bbStringPad(const BBString *str, BBInt len, BBStringSide side);
00452 
00464 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
00465 
00478 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
00479 
00485 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
00486 
00487 
00488 
00493 #define BBSTRING_NOTFOUND   (-1)
00494 
00511 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
00512 
00527 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
00528 
00546 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
00547 
00561 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
00562 
00570 BBString* bbStringToLower(const BBString *str);
00571 
00579 BBString* bbStringToUpper(const BBString *str);
00580 
00586 BBInt bbStringToInt(const BBString *str);
00587 
00593 BBFloat bbStringToFloat(const BBString *str);
00594 
00600 BBDouble bbStringToDouble(const BBString *str);
00601 
00608 void bbStringToLong(const BBString *str, BBLong *result);
00609 
00621 char* bbStringToCString(const BBString *str);
00622 
00631 BBChar* bbStringToWString(const BBString *str);
00632 
00644 char* bbStringToUTF8String(const BBString *str);
00645 
00656 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
00657 
00661 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
00662 
00663 
00664 
00665 //  Additions by BME
00666 
00680 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
00681 
00690 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
00691 
00699 BBString* bbStringReverse(const BBString *str);
00700 
00704 BBString* bbStringRotate(const BBString *str, BBInt count);
00705 
00723 BBString* bbStringTimes(const BBString *str, BBInt count);
00724 
00748 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
00749 
00756 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
00757 
00764 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
00765 
00774 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
00775 
00776 //  intern
00777 
00778 char* bbTmpCString(const BBString *str);
00779 BBChar* bbTmpWString(const BBString *str);
00780 char* bbTmpUTF8String(const BBString *str);
00781 
00782 BB_END_DECLS
00783 
00784 #endif  // BLITZ_STRING_H