Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreString.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreString.h
4 
5  QoreString Class Definition
6 
7  Qore Programming Language
8 
9  Copyright 2003 - 2013 David Nichols
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 
26 #ifndef _QORE_QORESTRING_H
27 
28 #define _QORE_QORESTRING_H
29 
30 #include <stdarg.h>
31 
32 #include <string>
33 
34 class DateTime;
35 class BinaryNode;
36 
38 
42 class QoreString {
43 protected:
45  struct qore_string_private* priv;
46 
47  DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, QoreString* extract = 0);
48  DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, const char* str, qore_size_t str_len, QoreString* extract = 0);
49  DLLLOCAL void splice_complex(qore_offset_t offset, ExceptionSink* xsink, QoreString* extract = 0);
50  DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink, QoreString* extract = 0);
51  DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, const QoreString* str, ExceptionSink* xsink, QoreString* extract = 0);
52  DLLLOCAL int substr_simple(QoreString* str, qore_offset_t offset) const;
53  DLLLOCAL int substr_simple(QoreString* str, qore_offset_t offset, qore_offset_t length) const;
54  DLLLOCAL int substr_complex(QoreString* str, qore_offset_t offset, ExceptionSink* xsink) const;
55  DLLLOCAL int substr_complex(QoreString* str, qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink) const;
56 
57  // writes a new QoreString with the characters reversed of the "this" QoreString
58  // assumes the encoding is the same and the length is 0
59  DLLLOCAL void concat_reverse(QoreString* targ) const;
60 
61  DLLLOCAL int snprintf(size_t size, const char* fmt, ...);
62  DLLLOCAL int vsnprintf(size_t size, const char* fmt, va_list args);
63  DLLLOCAL static int convert_encoding_intern(const char* src, qore_size_t src_len, const QoreEncoding* from, QoreString& targ, const QoreEncoding* to, ExceptionSink* xsink);
64 
65 public:
67  DLLEXPORT QoreString();
68 
70  DLLEXPORT QoreString(bool b);
71 
73  DLLEXPORT QoreString(const char* str);
74 
76  DLLEXPORT QoreString(const char* str, const QoreEncoding* new_qorecharset);
77 
79  DLLEXPORT QoreString(const QoreEncoding* new_qorecharset);
80 
82  DLLEXPORT QoreString(const char* str, qore_size_t len, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
83 
85  DLLEXPORT QoreString(const std::string& str, const QoreEncoding* new_encoding = QCS_DEFAULT);
86 
88  DLLEXPORT QoreString(char c);
89 
91  DLLEXPORT QoreString(const QoreString& str);
92 
94  DLLEXPORT QoreString(const QoreString* str);
95 
97  DLLEXPORT QoreString(const QoreString* str, qore_size_t len);
98 
100  DLLEXPORT QoreString(int64 i);
101 
103  DLLEXPORT QoreString(double f);
104 
106  DLLEXPORT QoreString(const DateTime* date);
107 
109  DLLEXPORT QoreString(const BinaryNode* bin);
110 
112  DLLEXPORT QoreString(const BinaryNode* bin, qore_size_t maxlinelen);
113 
115  DLLEXPORT QoreString(char* nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding* enc);
116 
118  DLLEXPORT ~QoreString();
119 
121 
123  DLLEXPORT qore_size_t length() const;
124 
126  DLLEXPORT void set(const char* str, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
127 
129  DLLEXPORT void set(const std::string& str, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
130 
132  DLLEXPORT void set(const QoreString* str);
133 
135  DLLEXPORT void set(const QoreString& str);
136 
138  DLLEXPORT void setEncoding(const QoreEncoding* new_encoding);
139 
141  DLLEXPORT void concatAndHTMLEncode(const char* str);
142 
144  DLLEXPORT void concatAndHTMLDecode(const QoreString* str);
145 
147  DLLEXPORT void concatEscape(const QoreString* str, char c, char esc_char, ExceptionSink* xsink);
148 
150  DLLEXPORT void concatEscape(const char* str, char c, char esc_char = '\\');
151 
153  DLLEXPORT void concatAndHTMLEncode(const QoreString* , ExceptionSink* xsink);
154 
156  DLLEXPORT void concat(const QoreString* str, ExceptionSink* xsink);
157 
159 
165  DLLEXPORT int concat(const QoreString& str, qore_offset_t pos, ExceptionSink* xsink);
166 
168 
175  DLLEXPORT int concat(const QoreString& str, qore_offset_t pos, qore_offset_t len, ExceptionSink* xsink);
176 
178 
183  DLLEXPORT void concat(const QoreString* str, qore_size_t size, ExceptionSink* xsink);
184 
186  DLLEXPORT void concatBase64(const char* buf, qore_size_t size);
187 
189  DLLEXPORT void concatBase64(const BinaryNode* bin);
190 
192  DLLEXPORT void concatBase64(const QoreString* str);
193 
195  DLLEXPORT void concatBase64(const char* buf, qore_size_t size, qore_size_t maxlinelen);
196 
198  DLLEXPORT void concatBase64(const BinaryNode* bin, qore_size_t maxlinelen);
199 
201  DLLEXPORT void concatBase64(const QoreString* str, qore_size_t maxlinelen);
202 
204 
208  DLLEXPORT BinaryNode* parseBase64(ExceptionSink* xsink) const;
209 
211 
215  DLLEXPORT QoreString* parseBase64ToString(ExceptionSink* xsink) const;
216 
218 
223  DLLEXPORT QoreString* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
224 
226  DLLEXPORT void concatHex(const char* buf, qore_size_t size);
227 
229  DLLEXPORT void concatHex(const BinaryNode* bin);
230 
232  DLLEXPORT void concatHex(const QoreString* str);
233 
235 
239  DLLEXPORT BinaryNode* parseHex(ExceptionSink* xsink) const;
240 
242  DLLEXPORT void concat(const DateTime* d);
243 
245  DLLEXPORT void concatISO8601DateTime(const DateTime* d);
246 
248  DLLEXPORT void concat(const char* str);
249 
251  DLLEXPORT void concat(const std::string& str);
252 
254  DLLEXPORT void concat(const char* str, qore_size_t size);
255 
257  DLLEXPORT void concat(const char c);
258 
260 
265  DLLEXPORT int compareSoft(const QoreString* str, ExceptionSink* xsink) const;
266 
268 
272  DLLEXPORT int compare(const QoreString* str) const;
273 
275 
279  DLLEXPORT int compare(const char* str) const;
280 
282  DLLEXPORT void terminate(qore_size_t size);
283 
285 
287  DLLEXPORT void reserve(qore_size_t size);
288 
290 
292  DLLEXPORT int sprintf(const char* fmt, ...);
293 
295 
297  DLLEXPORT int vsprintf(const char* fmt, va_list args);
298 
300 
302  DLLEXPORT void take(char* str);
303 
305  DLLEXPORT void take(char* str, const QoreEncoding* enc);
307 
308  DLLEXPORT void take(char* str, qore_size_t size);
309 
311  DLLEXPORT void take(char* str, qore_size_t size, const QoreEncoding* enc);
312 
314  DLLEXPORT void takeAndTerminate(char* str, qore_size_t size);
315 
317  DLLEXPORT void takeAndTerminate(char* str, qore_size_t size, const QoreEncoding* enc);
318 
320 
325  DLLEXPORT QoreString* convertEncoding(const QoreEncoding* nccs, ExceptionSink* xsink) const;
326 
328 
331  DLLEXPORT char* giveBuffer();
332 
334  DLLEXPORT void clear();
335 
337 
339  DLLEXPORT void reset();
340 
342 
344  DLLEXPORT void replaceAll(const char* old_str, const char* new_str);
345 
347 
349  DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const char* str);
350 
352 
357  DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString* str);
358 
360 
362  DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString* str, ExceptionSink* xsink);
363 
365 
367  DLLEXPORT void replaceChar(qore_size_t offset, char c);
368 
370 
374  DLLEXPORT void splice(qore_offset_t offset, ExceptionSink* xsink);
375 
377 
382  DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
383 
385 
391  DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* strn, ExceptionSink* xsink);
392 
394 
400  DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const QoreString& str, ExceptionSink* xsink);
401 
403 
408  DLLEXPORT QoreString* extract(qore_offset_t offset, ExceptionSink* xsink);
409 
411 
418 
420 
427  DLLEXPORT QoreString* extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* strn, ExceptionSink* xsink);
428 
430 
435  DLLEXPORT QoreString* substr(qore_offset_t offset, ExceptionSink* xsink) const;
436 
438 
444  DLLEXPORT QoreString* substr(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink) const;
445 
447  DLLEXPORT qore_size_t chomp();
448 
450  DLLEXPORT const QoreEncoding* getEncoding() const;
451 
453  DLLEXPORT QoreString* copy() const;
454 
456 
458  DLLEXPORT void tolwr();
459 
461 
463  DLLEXPORT void toupr();
464 
466  DLLEXPORT qore_size_t strlen() const;
467 
469  DLLEXPORT qore_size_t size() const;
470 
472  DLLEXPORT qore_size_t capacity() const;
473 
475  DLLEXPORT const char* getBuffer() const;
476 
478 
480  DLLEXPORT void allocate(unsigned requested_size);
481 
483 
489  DLLEXPORT int insertch(char c, qore_size_t pos, unsigned times);
490 
492 
497  DLLEXPORT int insert(const char* str, qore_size_t pos);
498 
500  DLLEXPORT void addch(char c, unsigned times);
501 
503 
509  DLLEXPORT void concatUTF8FromUnicode(unsigned code);
510 
512 
516  DLLEXPORT int concatUnicode(unsigned code, ExceptionSink* xsink);
517 
519 
523  DLLEXPORT int concatUnicode(unsigned code);
524 
526 
529  DLLEXPORT QoreString* reverse() const;
530 
532  DLLEXPORT void trim_trailing(const char* chars = 0);
533 
535  DLLEXPORT void trim_leading(const char* chars = 0);
536 
538  DLLEXPORT void trim(const char* chars = 0);
539 
541  DLLEXPORT void trim_trailing(char c);
542 
544  DLLEXPORT void trim_single_trailing(char c);
545 
547  DLLEXPORT void trim_leading(char c);
548 
550  DLLEXPORT void trim_single_leading(char c);
551 
553  DLLEXPORT void trim(char c);
554 
556 
560  DLLEXPORT unsigned int getUnicodePointFromUTF8(qore_offset_t offset = 0) const;
561 
563 
568  DLLEXPORT unsigned int getUnicodePoint(qore_offset_t offset, ExceptionSink* xsink) const;
569 
571  DLLEXPORT void prepend(const char* str);
572 
574  DLLEXPORT void prepend(const char* str, qore_size_t size);
575 
577  DLLEXPORT QoreString& operator=(const QoreString& other);
578 
580  DLLEXPORT QoreString& operator=(const char* other);
581 
583  DLLEXPORT QoreString& operator=(const std::string& other);
584 
586  DLLEXPORT bool operator==(const QoreString& other) const;
587 
589  DLLEXPORT bool operator==(const std::string& other) const;
590 
592  DLLEXPORT bool operator==(const char* other) const;
593 
595 
600  DLLEXPORT char operator[](qore_offset_t pos) const;
601 
603  DLLEXPORT QoreString& operator+=(const char* str);
604 
606  DLLEXPORT QoreString& operator+=(const std::string& str);
607 
609  DLLEXPORT bool empty() const;
610 
612  DLLEXPORT qore_offset_t index(const QoreString& needle, qore_offset_t pos, ExceptionSink* xsink) const;
613 
615  DLLEXPORT qore_offset_t bindex(const QoreString& needle, qore_offset_t pos) const;
616 
618  DLLEXPORT qore_offset_t bindex(const char* needle, qore_offset_t pos) const;
619 
621  DLLEXPORT qore_offset_t bindex(const std::string& needle, qore_offset_t pos) const;
622 
624 
630  DLLEXPORT qore_offset_t rindex(const QoreString& needle, qore_offset_t pos, ExceptionSink* xsink) const;
631 
633  DLLEXPORT qore_offset_t brindex(const QoreString& needle, qore_offset_t pos) const;
634 
636  DLLEXPORT qore_offset_t brindex(const char* needle, qore_offset_t pos) const;
637 
639  DLLEXPORT qore_offset_t brindex(const std::string& needle, qore_offset_t pos) const;
640 
642  DLLEXPORT qore_offset_t find(char c, qore_offset_t pos = 0) const;
643 
645  DLLEXPORT qore_offset_t rfind(char c, qore_offset_t pos = -1) const;
646 
648 
650  DLLEXPORT bool isDataPrintableAscii() const;
651 
653 
655  DLLEXPORT bool isDataAscii() const;
656 
658  DLLEXPORT int64 toBigInt() const;
659 
661 
666  DLLEXPORT qore_offset_t getByteOffset(qore_size_t i, ExceptionSink* xsink) const;
667 
668  // concatenates a qorestring without converting encodings - internal only
669  DLLLOCAL void concat(const QoreString* str);
670 
671  // private constructor
672  DLLLOCAL QoreString(struct qore_string_private* p);
673 };
674 
675 DLLEXPORT QoreString* checkEncoding(const QoreString* str, const QoreEncoding* enc, ExceptionSink* xsink);
676 
677 class QoreStringMaker : public QoreString {
678 private:
680  DLLLOCAL QoreStringMaker(const QoreStringMaker& str);
681 
683  DLLLOCAL QoreStringMaker& operator=(const QoreStringMaker&);
684 
685 public:
687 
689  DLLEXPORT QoreStringMaker(const char* fmt, ...);
690 
692 
694  DLLEXPORT QoreStringMaker(const QoreEncoding* enc, const char* fmt, ...);
695 };
696 
698 
707 class TempString {
708 private:
709  QoreString* str;
710 
712  TempString(const TempString& );
713 
715  TempString& operator=(const TempString& );
716 
718  void* operator new(size_t);
719 
720 public:
722  DLLLOCAL TempString() : str(new QoreString) {
723  }
724 
726  DLLLOCAL TempString(const QoreEncoding* enc) : str(new QoreString(enc)) {
727  }
728 
730  DLLLOCAL TempString(QoreString* s) {
731  str = s;
732  }
733 
735  DLLLOCAL ~TempString() {
736  delete str;
737  }
738 
740  DLLLOCAL QoreString* operator->(){ return str; };
741 
743  DLLLOCAL QoreString* operator*() { return str; };
744 
746  DLLLOCAL operator bool() const { return str != 0; }
747 
749  DLLLOCAL QoreString* release() { QoreString* rv = str; str = 0; return rv; }
750 };
751 
753 
766 private:
767  QoreString* str;
768  bool temp;
769 
771  DLLLOCAL TempEncodingHelper(const TempEncodingHelper& );
772 
774  DLLLOCAL TempEncodingHelper& operator=(const TempEncodingHelper& );
775 
777  DLLLOCAL void* operator new(size_t);
778 
780  DLLLOCAL void discard_intern() {
781  if (temp && str)
782  delete str;
783  }
784 
786 
791  DLLLOCAL void set_intern(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
792  if (s->getEncoding() != qe) {
793  str = s->convertEncoding(qe, xsink);
794  temp = true;
795  }
796  else {
797  str = const_cast<QoreString* >(s);
798  temp = false;
799  }
800  }
801 
802 public:
804 
809  DLLLOCAL TempEncodingHelper(const QoreString& s, const QoreEncoding* qe, ExceptionSink* xsink) {
810  set_intern(&s, qe, xsink);
811  }
812 
814 
819  DLLLOCAL TempEncodingHelper(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
820  set_intern(s, qe, xsink);
821  }
822 
824  DLLLOCAL TempEncodingHelper() : str(0), temp(false) {
825  }
826 
828  DLLLOCAL ~TempEncodingHelper() {
829  discard_intern();
830  }
831 
833 
842  DLLLOCAL int set(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
843  discard_intern();
844 
845  set_intern(s, qe, xsink);
846  return str != 0;
847  }
848 
850  DLLLOCAL bool is_temp() const {
851  return temp;
852  }
853 
855  DLLLOCAL const QoreString* operator->(){ return str; };
856 
858  DLLLOCAL const QoreString* operator*() { return str; };
859 
861 
864  DLLLOCAL operator bool() const { return str != 0; }
865 
867 
870  DLLLOCAL char* giveBuffer() {
871  if (!str)
872  return 0;
873  if (temp)
874  return str->giveBuffer();
875  return strdup(str->getBuffer());
876  }
877 };
878 
879 #endif
880