BlitzMax Extended  0.8.16
Pushing Blitz to the Max.
blitz_string.h
Go to the documentation of this file.
1 
9 #ifndef BLITZ_STRING_H
10 #define BLITZ_STRING_H
11 
12 #include "_common.h"
13 
14 #include "blitz_array.h"
15 #include "blitz_object.h"
16 
17 BB_BEGIN_DECLS
18 
19 // Used internally to calculate the amount of memory necessary
20 // to allocate a string with a certain length.
21 #define BBSTRING_SIZE_CALC(len) (sizeof (BBString) + (len) * sizeof (BBChar))
22 
31 #define BBSTRING_SIZE(str) BBSTRING_SIZE_CALC((str)->length)
32 
33 #ifndef BB_ENABLE_EXPERIMENTAL
34 
39 # define BBSTRING_MAX_LENGTH ((BBSize)(65536 / sizeof (BBChar) - sizeof (BBString)))
40 #else
41 # define BBSTRING_MAX_LENGTH ((BBSize)(BBSIZE_MAX / sizeof (BBChar)) - sizeof (BBString))
42 #endif // BB_ENABLE_EXPERIMENTAL
43 
61 #define BBSTRING_ALLOC(len) \
62  ({ \
63  (BBString *) \
64  bbGCAllocObject( \
65  BBSTRING_SIZE_CALC(len), \
66  &bbStringClass, \
67  BBGC_ATOMIC \
68  ); \
69  })
70 
71 /* Creates a string from a BBChar buffer
72  *
73  * \param buffer A buffer containing UCS-2 encoded string data
74  * \param len The length of the buffer and the new string
75  * \returns A pointer to a newly allocated string with the given contents
76  */
77 #define BBSTRING_FROMBUFFER(buffer, len) \
78  ({ \
79  BB_ASSERT((len) > 0 && (len) < BBSTRING_MAX_LENGTH, "Invalid buffer length"); \
80  BBString *_str = BBSTRING_ALLOC((len)); \
81  _str->length = (len); \
82  memcpy(_str->buf, (buffer), (len) * sizeof (BBChar)); \
83  _str; \
84  })
85 
103 struct BBString
104 {
111  int refs;
112 
120  BBSize length;
121 
124 };
125 
131 extern BBClass bbStringClass;
132 
142 extern BBString bbEmptyString;
143 
150 #define BBNULLSTRING (&bbEmptyString)
151 
152 #ifdef BB_ENABLE_HELPER
153 
154 bool bb_string_buffer_equals(const BBChar * restrict str_buf,
155  const BBChar * restrict cmp_buf,
156  BBSize len) __attribute__ ((pure));
157 
158 #endif // BB_ENABLE_HELPER
159 
160 
161 
162 // Built-in
163 
173 BBString* bbStringToString(const BBString *str);
174 
191 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure));
192 
206 BBString* bbStringClone(const BBString *str);
207 
208 
209 
210 // API
211 
224 BBString* bbStringNew(BBInt len);
225 
233 BBString* bbStringFromChar(BBInt codepoint);
234 
242 BBString* bbStringFromInt(BBInt number);
243 
249 BBString* bbStringFromLong(BBLong number);
250 
256 BBString* bbStringFromFloat(BBFloat number);
257 
263 BBString* bbStringFromDouble(BBDouble number);
264 
278 BBString* bbStringFromBytes(const char *buf, BBInt size);
279 
293 BBString* bbStringFromShorts(const BBShort *buf, BBInt size);
294 
312 BBString* bbStringFromInts(const BBInt *buf, BBInt size);
313 
326 BBString* bbStringFromArray(const BBArray *arr);
327 
335 BBString* bbStringFromCString(const char *c_str);
336 
344 BBString* bbStringFromWString(const BBChar *w_str);
345 
355 BBString* bbStringFromUTF8String(const char *utf8_str);
356 
357 
365 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
366 
373 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
374 
387 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure));
388 
389 
390 
401 BBString* bbStringConcat(const BBString *str, const BBString *rhs);
402 
403 
412 {
415 
418 
421 };
422 
423 typedef enum BBStringSide BBStringSide;
424 
425 #define BBSTRING_SIDE_ISVALID(side) \
426  ((side) >= BBSTRING_SIDE_LEFT && (side) <= BBSTRING_SIDE_RIGHT)
427 
440 BBString* bbStringTrim(const BBString *str, BBStringSide side);
441 
456 BBString* bbStringPad(const BBString *str, BBInt len, BBStringSide side);
457 
469 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
470 
483 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
484 
490 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
491 
492 
493 
498 #define BBSTRING_NOTFOUND (-1)
499 
516 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
517 
532 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
533 
551 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
552 
566 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
567 
575 BBString* bbStringToLower(const BBString *str);
576 
584 BBString* bbStringToUpper(const BBString *str);
585 
591 BBInt bbStringToInt(const BBString *str);
592 
598 BBFloat bbStringToFloat(const BBString *str);
599 
605 BBDouble bbStringToDouble(const BBString *str);
606 
613 void bbStringToLong(const BBString *str, BBLong *result);
614 
626 char* bbStringToCString(const BBString *str);
627 
636 BBChar* bbStringToWString(const BBString *str);
637 
649 char* bbStringToUTF8String(const BBString *str);
650 
661 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
662 
666 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
667 
668 
669 
670 // Additions by BME
671 
685 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
686 
695 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
696 
704 BBString* bbStringReverse(const BBString *str);
705 
719 BBString* bbStringRotate(const BBString *str, BBInt count);
720 
738 BBString* bbStringTimes(const BBString *str, BBInt count);
739 
763 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
764 
773 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
774 
783 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
784 
793 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
794 
795 // intern
796 
797 char* bbTmpCString(const BBString *str);
798 BBChar* bbTmpWString(const BBString *str);
799 char* bbTmpUTF8String(const BBString *str);
800 
801 BB_END_DECLS
802 
803 #endif // BLITZ_STRING_H