BlitzMax Extended  0.8.18
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 
26 #define BBSTRING_SIZE_CALC(len) (sizeof (BBString) + (len) * sizeof (BBChar))
27 
36 #define BBSTRING_SIZE(str) BBSTRING_SIZE_CALC((str)->length)
37 
38 #ifndef BB_ENABLE_EXPERIMENTAL
39 
44 # define BBSTRING_MAX_LENGTH ((BBSize)(BBSIZE_MAX / sizeof (BBChar) - sizeof (BBString)) / 2)
45 #else
46 # define BBSTRING_MAX_LENGTH ((BBSize)(BBSIZE_MAX / sizeof (BBChar)) - sizeof (BBString))
47 #endif // BB_ENABLE_EXPERIMENTAL
48 
67 #define BBSTRING_ALLOC(len) \
68  ({ \
69  (BBString *) \
70  bbGCAllocObject( \
71  BBSTRING_SIZE_CALC(len), \
72  &bbStringClass, \
73  BBGC_ATOMIC \
74  ); \
75  })
76 
86 #define BBSTRING_NEW(len) \
87  ({ \
88  const BBSize _len = (len); \
89  BBString *_str = BBSTRING_ALLOC(_len); \
90  _str->length = _len; \
91  _str; \
92  })
93 
94 /* \internal
95  *
96  * \brief Creates a string from a \c BBChar buffer
97  *
98  * \param buffer A buffer containing \c UCS-2 encoded string data
99  * \param len The length of the buffer and the new string
100  * \returns A pointer to a newly allocated string with the given contents
101  */
102 #define BBSTRING_FROMBUFFER(buffer, len) \
103  ({ \
104  const BBSize _len = (len); \
105  BB_ASSERT(_len > 0 && _len < BBSTRING_MAX_LENGTH, "Invalid buffer length"); \
106  BBString *_str = BBSTRING_ALLOC(_len); \
107  _str->length = _len; \
108  memcpy(_str->buf, (buffer), _len * sizeof (BBChar)); \
109  _str; \
110  })
111 
129 struct BBString
130 {
137  int refs;
138 
146  BBSize length;
147 
150 };
151 
159 extern BBClass bbStringClass;
160 
169 extern BBString bbEmptyString;
170 
171 
172 #ifdef BB_INCLUDE_INTERNALS
173 
185 BBBool bb_strbuf_equals(const BBChar * restrict str_buf,
186  const BBChar * restrict cmp_buf,
187  const BBSize len) __attribute__ ((pure));
188 
189 #define BBSTRING_MAX_MATCHES(str, sub) (((str)->length / (sub)->length) + 1)
190 
191 BBSize bb_strbuf_findall(const BBChar * restrict str_buf,
192  BBSize str_len,
193  const BBChar * restrict sub_buf,
194  BBSize sub_len,
195  BBSize * const matches)
196  __attribute__ ((nonnull (1, 3, 5)));
197 
198 #endif // BB_INCLUDE_INTERNALS
199 
200 // API //
201 
210 #define BBNULLSTRING (&bbEmptyString)
211 
221 BBString* bbStringToString(const BBString *str);
222 
239 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure));
240 
254 BBString* bbStringClone(const BBString *str);
255 
256 
257 
270 BBString* bbStringNew(BBInt len);
271 
279 BBString* bbStringFromChar(BBInt codepoint);
280 
288 BBString* bbStringFromInt(BBInt number);
289 
295 BBString* bbStringFromLong(BBLong number);
296 
302 BBString* bbStringFromFloat(BBFloat number);
303 
309 BBString* bbStringFromDouble(BBDouble number);
310 
324 BBString* bbStringFromBytes(const char *buf, BBInt size);
325 
339 BBString* bbStringFromShorts(const BBShort *buf, BBInt size);
340 
358 BBString* bbStringFromInts(const BBInt *buf, BBInt size);
359 
372 BBString* bbStringFromArray(const BBArray *arr);
373 
381 BBString* bbStringFromCString(const char *c_str);
382 
390 BBString* bbStringFromWString(const BBChar *w_str);
391 
401 BBString* bbStringFromUTF8String(const char *utf8_str);
402 
403 
411 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
412 
419 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
420 
433 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure));
434 
435 
436 
447 BBString* bbStringConcat(const BBString *str, const BBString *rhs);
448 
449 
458 {
461 
464 
467 };
468 
469 typedef enum BBStringSide BBStringSide;
470 
483 BBString* bbStringTrim(const BBString *str, BBStringSide side);
484 
499 BBString* bbStringPad(const BBString *str, BBInt len, BBStringSide side);
500 
512 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
513 
526 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
527 
533 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
534 
535 
536 
541 #define BBSTRING_NOTFOUND (-1)
542 
559 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
560 
575 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
576 
593 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
594 
608 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
609 
617 BBString* bbStringToLower(const BBString *str);
618 
626 BBString* bbStringToUpper(const BBString *str);
627 
633 BBInt bbStringToInt(const BBString *str);
634 
640 BBFloat bbStringToFloat(const BBString *str);
641 
647 BBDouble bbStringToDouble(const BBString *str);
648 
655 void bbStringToLong(const BBString *str, BBLong *result);
656 
668 char* bbStringToCString(const BBString *str);
669 
678 BBChar* bbStringToWString(const BBString *str);
679 
691 char* bbStringToUTF8String(const BBString *str);
692 
703 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
704 
708 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
709 
710 
711 
712 // Additions by BME
713 
727 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
728 
737 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
738 
746 BBString* bbStringReverse(const BBString *str);
747 
761 BBString* bbStringRotate(const BBString *str, BBInt count);
762 
780 BBString* bbStringTimes(const BBString *str, BBInt count);
781 
805 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
806 
815 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
816 
825 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
826 
835 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
836 
837 // intern
838 
839 char* bbTmpCString(const BBString *str);
840 BBChar* bbTmpWString(const BBString *str);
841 char* bbTmpUTF8String(const BBString *str);
842 
843 BB_END_DECLS
844 
845 #endif // BLITZ_STRING_H