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 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 BBBool bb_strbuf_equals(str_buf, cmp_buf, len)
195  const BBChar * restrict str_buf;
196  const BBChar * restrict cmp_buf;
197  const BBSize len;
198 {
199  --str_buf; --cmp_buf;
200 
201  for (BBSize pos = 0; pos < len; ++pos)
202  {
203  if (*++str_buf != *++cmp_buf) {
204  return BBFALSE;
205  }
206  }
207 
208  return BBTRUE;
209 } __attribute__ ((pure))
210 
211 
222 inline BBChar* bb_strbuf_copy(str_buf, dst_buf, len)
223  const BBChar * restrict str_buf;
224  BBChar * restrict dst_buf;
225  const BBSize len;
226 {
227  return memcpy(dst_buf, str_buf, len * sizeof (BBChar));
228 }
229 
230 #define BBSTRING_MAX_MATCHES(str, sub) (((str)->length / (sub)->length) + 1)
231 
232 BBSize bb_strbuf_findall(const BBChar * restrict str_buf,
233  BBSize str_len,
234  const BBChar * restrict sub_buf,
235  BBSize sub_len,
236  BBSize * const matches)
237  __attribute__ ((nonnull(1,3,5)));
238 
239 #endif // BB_ENABLE_HELPER
240 
241 
242 // API //
243 
253 BBString* bbStringToString(const BBString *str);
254 
271 BBInt bbStringCompare(const BBString *str, const BBString *rhs) __attribute__ ((pure));
272 
286 BBString* bbStringClone(const BBString *str);
287 
288 
289 
302 BBString* bbStringNew(BBInt len);
303 
311 BBString* bbStringFromChar(BBInt codepoint);
312 
320 BBString* bbStringFromInt(BBInt number);
321 
327 BBString* bbStringFromLong(BBLong number);
328 
334 BBString* bbStringFromFloat(BBFloat number);
335 
341 BBString* bbStringFromDouble(BBDouble number);
342 
356 BBString* bbStringFromBytes(const char *buf, BBInt size);
357 
371 BBString* bbStringFromShorts(const BBShort *buf, BBInt size);
372 
390 BBString* bbStringFromInts(const BBInt *buf, BBInt size);
391 
404 BBString* bbStringFromArray(const BBArray *arr);
405 
413 BBString* bbStringFromCString(const char *c_str);
414 
422 BBString* bbStringFromWString(const BBChar *w_str);
423 
433 BBString* bbStringFromUTF8String(const char *utf8_str);
434 
435 
443 BBBool bbStringStartsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
444 
451 BBBool bbStringEndsWith(const BBString *str, const BBString *sub) __attribute__ ((pure));
452 
465 BBBool bbStringContains(const BBString *str, const BBString *sub) __attribute__ ((pure));
466 
467 
468 
479 BBString* bbStringConcat(const BBString *str, const BBString *rhs);
480 
481 
490 {
493 
496 
499 };
500 
501 typedef enum BBStringSide BBStringSide;
502 
515 BBString* bbStringTrim(const BBString *str, BBStringSide side);
516 
531 BBString* bbStringPad(const BBString *str, BBInt len, BBStringSide side);
532 
544 BBString* bbStringSlice(const BBString *str, BBInt begin, BBInt end);
545 
558 BBString* bbStringReplace(const BBString *str, const BBString *sub, const BBString *replacement);
559 
565 BBInt bbStringAsc(const BBString *str) __attribute__ ((pure));
566 
567 
568 
573 #define BBSTRING_NOTFOUND (-1)
574 
591 BBInt bbStringFind(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
592 
607 BBInt bbStringFindLast(const BBString *str, const BBString *sub, BBInt index) __attribute__ ((pure));
608 
625 BBArray* bbStringFindAll(const BBString* str, const BBString *sub, BBInt index);
626 
640 BBInt bbStringFindAny(const BBString *str, const BBStringArray *all_subs, BBInt index) __attribute__ ((pure));
641 
649 BBString* bbStringToLower(const BBString *str);
650 
658 BBString* bbStringToUpper(const BBString *str);
659 
665 BBInt bbStringToInt(const BBString *str);
666 
672 BBFloat bbStringToFloat(const BBString *str);
673 
679 BBDouble bbStringToDouble(const BBString *str);
680 
687 void bbStringToLong(const BBString *str, BBLong *result);
688 
700 char* bbStringToCString(const BBString *str);
701 
710 BBChar* bbStringToWString(const BBString *str);
711 
723 char* bbStringToUTF8String(const BBString *str);
724 
735 BBStringArray* bbStringSplit(const BBString *str, const BBString *separator);
736 
740 BBString* bbStringJoin(const BBString *str, const BBStringArray *bits);
741 
742 
743 
744 // Additions by BME
745 
759 BBBool bbStringEquals(const BBString *str, const BBString *rhs) __attribute__ ((pure));
760 
769 BBInt bbStringCount(const BBString *str, const BBString *sub) __attribute__ ((pure));
770 
778 BBString* bbStringReverse(const BBString *str);
779 
793 BBString* bbStringRotate(const BBString *str, BBInt count);
794 
812 BBString* bbStringTimes(const BBString *str, BBInt count);
813 
837 BBString* bbStringSub(const BBString *str, BBInt index, BBInt len);
838 
847 BBBool bbStringIsLower(const BBString *str) __attribute__ ((pure));
848 
857 BBBool bbStringIsUpper(const BBString *str) __attribute__ ((pure));
858 
867 BBBool bbStringIsASCII(const BBString *str) __attribute__ ((pure));
868 
869 // intern
870 
871 char* bbTmpCString(const BBString *str);
872 BBChar* bbTmpWString(const BBString *str);
873 char* bbTmpUTF8String(const BBString *str);
874 
875 BB_END_DECLS
876 
877 #endif // BLITZ_STRING_H