BlitzMax Extended  0.8.17
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 BBChar buffer
97  *
98  * \param buffer A buffer containing 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 
179 #define BBNULLSTRING (&bbEmptyString)
180 
181 #ifdef BB_ENABLE_HELPER
182 
194 inline BBChar* bb_strbuf_copy(str_buf, dst_buf, len)
195  const BBChar * restrict str_buf;
196  BBChar * restrict dst_buf;
197  const BBSize len;
198 {
199  return memcpy(dst_buf, str_buf, len * sizeof (BBChar));
200 } __attribute__ ((nonnull (1,2)))
201 
202 
213 bool bb_strbuf_equals(const BBChar * restrict str_buf,
214  const BBChar * restrict cmp_buf,
215  BBSize len) __attribute__ ((pure));
216 
217 #define BBSTRING_MAX_MATCHES(str, sub) (((str)->length / (sub)->length) + 1)
218 
219 BBSize bb_strbuf_findall(
220  const BBChar * restrict str_buf,
221  BBSize str_len,
222  const BBChar * restrict sub_buf,
223  BBSize sub_len,
224  BBSize * const matches
225  ) __attribute__ ((pure, nonnull(1,3)));
226 
227 #endif // BB_ENABLE_HELPER
228 
229 
230 // API //
231 
241 BBString* bbStringToString(const BBString *str);
242 
259 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure));
260 
274 BBString* bbStringClone(const BBString *str);
275 
276 
277 
290 BBString* bbStringNew(BBInt len);
291 
299 BBString* bbStringFromChar(BBInt codepoint);
300 
308 BBString* bbStringFromInt(BBInt number);
309 
315 BBString* bbStringFromLong(BBLong number);
316 
322 BBString* bbStringFromFloat(BBFloat number);
323 
329 BBString* bbStringFromDouble(BBDouble number);
330 
344 BBString* bbStringFromBytes(const char *buf, BBInt size);
345 
359 BBString* bbStringFromShorts(const BBShort *buf, BBInt size);
360 
378 BBString* bbStringFromInts(const BBInt *buf, BBInt size);
379 
392 BBString* bbStringFromArray(const BBArray *arr);
393 
401 BBString* bbStringFromCString(const char *c_str);
402 
410 BBString* bbStringFromWString(const BBChar *w_str);
411 
421 BBString* bbStringFromUTF8String(const char *utf8_str);
422 
423 
431 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
432 
439 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
440 
453 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure));
454 
455 
456 
467 BBString* bbStringConcat(const BBString *str, const BBString *rhs);
468 
469 
478 {
481 
484 
487 };
488 
489 typedef enum BBStringSide BBStringSide;
490 
503 BBString* bbStringTrim(const BBString *str, BBStringSide side);
504 
519 BBString* bbStringPad(const BBString *str, BBInt len, BBStringSide side);
520 
532 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
533 
546 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
547 
553 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
554 
555 
556 
561 #define BBSTRING_NOTFOUND (-1)
562 
579 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
580 
595 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
596 
613 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
614 
628 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
629 
637 BBString* bbStringToLower(const BBString *str);
638 
646 BBString* bbStringToUpper(const BBString *str);
647 
653 BBInt bbStringToInt(const BBString *str);
654 
660 BBFloat bbStringToFloat(const BBString *str);
661 
667 BBDouble bbStringToDouble(const BBString *str);
668 
675 void bbStringToLong(const BBString *str, BBLong *result);
676 
688 char* bbStringToCString(const BBString *str);
689 
698 BBChar* bbStringToWString(const BBString *str);
699 
711 char* bbStringToUTF8String(const BBString *str);
712 
723 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
724 
728 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
729 
730 
731 
732 // Additions by BME
733 
747 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
748 
757 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
758 
766 BBString* bbStringReverse(const BBString *str);
767 
781 BBString* bbStringRotate(const BBString *str, BBInt count);
782 
800 BBString* bbStringTimes(const BBString *str, BBInt count);
801 
825 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
826 
835 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
836 
845 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
846 
855 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
856 
857 // intern
858 
859 char* bbTmpCString(const BBString *str);
860 BBChar* bbTmpWString(const BBString *str);
861 char* bbTmpUTF8String(const BBString *str);
862 
863 BB_END_DECLS
864 
865 #endif // BLITZ_STRING_H