Qore Programming Language  0.8.3
include/qore/QoreString.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   QoreString.h
00004 
00005   QoreString Class Definition
00006 
00007   Qore Programming Language
00008 
00009   Copyright 2003 - 2011 David Nichols
00010 
00011   This library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU Lesser General Public
00013   License as published by the Free Software Foundation; either
00014   version 2.1 of the License, or (at your option) any later version.
00015 
00016   This library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019   Lesser General Public License for more details.
00020 
00021   You should have received a copy of the GNU Lesser General Public
00022   License along with this library; if not, write to the Free Software
00023   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00024 */
00025 
00026 #ifndef _QORE_QORESTRING_H
00027 
00028 #define _QORE_QORESTRING_H
00029 
00030 #include <stdarg.h>
00031 
00032 #include <string>
00033 
00034 class DateTime;
00035 class BinaryNode;
00036 
00038 
00042 class QoreString {
00043 protected:
00045    struct qore_string_private *priv;
00046 
00047    DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, QoreString *extract = 0);
00048    DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, const char *str, qore_size_t str_len, QoreString *extract = 0);
00049    DLLLOCAL void splice_complex(qore_offset_t offset, ExceptionSink *xsink, QoreString *extract = 0);
00050    DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink, QoreString *extract = 0);
00051    DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, const QoreString *str, ExceptionSink *xsink, QoreString *extract = 0);
00052    DLLLOCAL int substr_simple(QoreString *str, qore_offset_t offset) const;
00053    DLLLOCAL int substr_simple(QoreString *str, qore_offset_t offset, qore_offset_t length) const;
00054    DLLLOCAL int substr_complex(QoreString *str, qore_offset_t offset, ExceptionSink *xsink) const;
00055    DLLLOCAL int substr_complex(QoreString *str, qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
00056 
00057    // writes a new QoreString with the characters reversed of the "this" QoreString
00058    // assumes the encoding is the same and the length is 0
00059    DLLLOCAL void concat_reverse(QoreString *targ) const;
00060 
00061    DLLLOCAL int snprintf(size_t size, const char *fmt, ...);
00062    DLLLOCAL int vsnprintf(size_t size, const char *fmt, va_list args);
00063    DLLLOCAL static int convert_encoding_intern(const char *src, qore_size_t src_len, const QoreEncoding *from, QoreString &targ, const QoreEncoding *to, ExceptionSink *xsink);
00064 
00065 public:
00067    DLLEXPORT QoreString();
00068 
00070    DLLEXPORT QoreString(bool b);
00071 
00073    DLLEXPORT QoreString(const char *str);
00074 
00076    DLLEXPORT QoreString(const char *str, const QoreEncoding *new_qorecharset);
00077 
00079    DLLEXPORT QoreString(const QoreEncoding *new_qorecharset);
00080 
00082    DLLEXPORT QoreString(const char *str, qore_size_t len, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
00083 
00085    DLLEXPORT QoreString(const std::string &str, const QoreEncoding *new_encoding = QCS_DEFAULT);
00086 
00088    DLLEXPORT QoreString(char c);
00089 
00091    DLLEXPORT QoreString(const QoreString &str);
00092 
00094    DLLEXPORT QoreString(const QoreString *str);
00095 
00097    DLLEXPORT QoreString(const QoreString *str, qore_size_t len);
00098 
00100    DLLEXPORT QoreString(int64 i);
00101 
00103    DLLEXPORT QoreString(double f);
00104 
00106    DLLEXPORT QoreString(const DateTime *date);
00107 
00109    DLLEXPORT QoreString(const BinaryNode *bin);
00110 
00112    DLLEXPORT QoreString(char *nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding *enc);
00113 
00115    DLLEXPORT ~QoreString();
00116 
00118 
00120    DLLEXPORT qore_size_t length() const;
00121 
00123    DLLEXPORT void set(const char *str, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
00124 
00126    DLLEXPORT void set(const QoreString *str);
00127 
00129    DLLEXPORT void set(const QoreString &str);
00130 
00132    DLLEXPORT void setEncoding(const QoreEncoding *new_encoding);
00133 
00135    DLLEXPORT void concatAndHTMLEncode(const char *str);
00136 
00138    DLLEXPORT void concatAndHTMLDecode(const QoreString *str);
00139 
00141    DLLEXPORT void concatEscape(const QoreString *str, char c, char esc_char, ExceptionSink *xsink);
00142 
00144    DLLEXPORT void concatEscape(const char *str, char c, char esc_char = '\\');
00145 
00147    DLLEXPORT void concatAndHTMLEncode(const QoreString *, ExceptionSink *xsink);
00148 
00150    DLLEXPORT void concat(const QoreString *str, ExceptionSink *xsink);
00151 
00153 
00158    DLLEXPORT void concat(const QoreString *str, qore_size_t size, ExceptionSink *xsink);
00159 
00161    DLLEXPORT void concatBase64(const char *buf, qore_size_t size);
00162 
00164    DLLEXPORT void concatBase64(const BinaryNode *bin);
00165 
00167    DLLEXPORT void concatBase64(const QoreString *str);
00168 
00170 
00174    DLLEXPORT BinaryNode *parseBase64(ExceptionSink *xsink) const;
00175 
00177 
00181    DLLEXPORT QoreString *parseBase64ToString(ExceptionSink *xsink) const;
00182 
00184    DLLEXPORT void concatHex(const char *buf, qore_size_t size);
00185 
00187    DLLEXPORT void concatHex(const BinaryNode *bin);
00188 
00190    DLLEXPORT void concatHex(const QoreString *str);
00191 
00193 
00197    DLLEXPORT BinaryNode *parseHex(ExceptionSink *xsink) const;
00198 
00200    DLLEXPORT void concat(const DateTime *d);
00201 
00203    DLLEXPORT void concatISO8601DateTime(const DateTime *d);
00204 
00206    DLLEXPORT void concat(const char *str);
00207 
00209    DLLEXPORT void concat(const std::string &str);
00210 
00212    DLLEXPORT void concat(const char *str, qore_size_t size);
00213 
00215    DLLEXPORT void concat(const char c);
00216 
00218 
00223    DLLEXPORT int compareSoft(const QoreString *str, ExceptionSink *xsink) const;
00224 
00226 
00230    DLLEXPORT int compare(const QoreString *str) const;
00231 
00233 
00237    DLLEXPORT int compare(const char *str) const;
00238 
00240    DLLEXPORT void terminate(qore_size_t size);
00241 
00243 
00245    DLLEXPORT void reserve(qore_size_t size);
00246 
00248 
00250    DLLEXPORT int sprintf(const char *fmt, ...);
00251 
00253 
00255    DLLEXPORT int vsprintf(const char *fmt, va_list args);
00256 
00258 
00260    DLLEXPORT void take(char *str);
00261 
00263    DLLEXPORT void take(char *str, const QoreEncoding *enc);
00265 
00266    DLLEXPORT void take(char *str, qore_size_t size);
00267 
00269    DLLEXPORT void take(char *str, qore_size_t size, const QoreEncoding *enc);
00270 
00272    DLLEXPORT void takeAndTerminate(char *str, qore_size_t size);
00273 
00275    DLLEXPORT void takeAndTerminate(char *str, qore_size_t size, const QoreEncoding *enc);
00276 
00278 
00283    DLLEXPORT QoreString *convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const;
00284 
00286 
00289    DLLEXPORT char *giveBuffer();
00290 
00292    DLLEXPORT void clear();
00293 
00295 
00297    DLLEXPORT void reset();
00298 
00300 
00302    DLLEXPORT void replaceAll(const char *old_str, const char *new_str);
00303 
00305 
00307    DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const char *str);
00308 
00310 
00315    DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString *str);
00316 
00318 
00320    DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString *str, ExceptionSink *xsink);
00321 
00323 
00327    DLLEXPORT void splice(qore_offset_t offset, ExceptionSink *xsink);
00328 
00330 
00335    DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
00336 
00338 
00344    DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
00345 
00347 
00353    DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const QoreString &str, ExceptionSink *xsink);
00354 
00356 
00361    DLLEXPORT QoreString *extract(qore_offset_t offset, ExceptionSink *xsink);
00362 
00364 
00370    DLLEXPORT QoreString *extract(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
00371 
00373 
00380    DLLEXPORT QoreString *extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
00381 
00383 
00388    DLLEXPORT QoreString *substr(qore_offset_t offset, ExceptionSink *xsink) const;
00389 
00391 
00397    DLLEXPORT QoreString *substr(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
00398 
00400    DLLEXPORT qore_size_t chomp();
00401 
00403    DLLEXPORT const QoreEncoding *getEncoding() const;
00404 
00406    DLLEXPORT QoreString *copy() const;
00407 
00409 
00411    DLLEXPORT void tolwr();
00412 
00414 
00416    DLLEXPORT void toupr();
00417 
00419    DLLEXPORT qore_size_t strlen() const;
00420 
00422    DLLEXPORT qore_size_t size() const;
00423 
00425    DLLEXPORT qore_size_t capacity() const;
00426 
00428    DLLEXPORT const char *getBuffer() const;
00429 
00430    // Make sure the internal buffer has at least expected size in bytes. 
00431    // Useful to eliminate repeated reallocate() when data are appended in a loop.
00432    DLLEXPORT void allocate(unsigned requested_size);
00433 
00435    DLLEXPORT void addch(char c, unsigned times);
00436 
00438 
00444    DLLEXPORT void concatUTF8FromUnicode(unsigned code);
00445 
00447 
00451    DLLEXPORT int concatUnicode(unsigned code, ExceptionSink *xsink);
00452 
00454 
00458    DLLEXPORT int concatUnicode(unsigned code);
00459 
00461 
00464    DLLEXPORT QoreString *reverse() const;
00465 
00467    DLLEXPORT void trim_trailing(const char *chars = 0);
00468 
00470    DLLEXPORT void trim_leading(const char *chars = 0);
00471 
00473    DLLEXPORT void trim(const char *chars = 0);
00474 
00476    DLLEXPORT void trim_trailing(char c);
00477 
00479    DLLEXPORT void trim_single_trailing(char c);
00480 
00482    DLLEXPORT void trim_leading(char c);
00483 
00485    DLLEXPORT void trim_single_leading(char c);
00486 
00488    DLLEXPORT void trim(char c);
00489 
00491 
00495    DLLEXPORT unsigned int getUnicodePointFromUTF8(qore_offset_t offset = 0) const;
00496 
00498 
00503    DLLEXPORT unsigned int getUnicodePoint(qore_offset_t offset, ExceptionSink *xsink) const;
00504 
00506    DLLEXPORT void prepend(const char *str);
00507 
00509    DLLEXPORT void prepend(const char *str, qore_size_t size);
00510 
00512    DLLEXPORT QoreString & operator=(const QoreString &other);
00513 
00515    DLLEXPORT bool operator==(const QoreString &other) const;
00516 
00518    DLLEXPORT bool operator==(const std::string &other) const;
00519 
00521    DLLEXPORT bool operator==(const char *other) const;
00522 
00524 
00527    DLLEXPORT char operator[](qore_offset_t pos) const;
00528 
00530    DLLEXPORT QoreString &operator+=(const char *str);
00531 
00533    DLLEXPORT QoreString &operator+=(const std::string &str);
00534 
00536    DLLEXPORT bool empty() const;
00537 
00539    DLLEXPORT qore_offset_t index(const QoreString &needle, qore_offset_t pos, ExceptionSink *xsink) const;
00540 
00542    DLLEXPORT qore_offset_t bindex(const QoreString &needle, qore_offset_t pos) const;
00543 
00545    DLLEXPORT qore_offset_t bindex(const char *needle, qore_offset_t pos) const;
00546 
00548    DLLEXPORT qore_offset_t bindex(const std::string &needle, qore_offset_t pos) const;
00549 
00551    DLLEXPORT qore_offset_t rindex(const QoreString &needle, qore_offset_t pos, ExceptionSink *xsink) const;
00552 
00554    DLLEXPORT qore_offset_t brindex(const QoreString &needle, qore_offset_t pos) const;
00555 
00557    DLLEXPORT qore_offset_t brindex(const char *needle, qore_offset_t pos) const;
00558 
00560    DLLEXPORT qore_offset_t brindex(const std::string &needle, qore_offset_t pos) const;
00561 
00562    // concatenates a qorestring without converting encodings - internal only
00563    DLLLOCAL void concat(const QoreString *str);
00564 
00565    // private constructor
00566    DLLLOCAL QoreString(struct qore_string_private *p);
00567 };
00568 
00569 DLLEXPORT QoreString *checkEncoding(const QoreString *str, const QoreEncoding *enc, ExceptionSink *xsink);
00570 
00572 
00581 class TempString {
00582 private:
00583    QoreString *str;
00584 
00586    TempString(const TempString &);
00587 
00589    TempString & operator=(const TempString &);
00590 
00592    void *operator new(size_t);
00593 
00594 public:
00596    DLLLOCAL TempString() : str(new QoreString) {
00597    }
00598 
00600    DLLLOCAL TempString(const QoreEncoding *enc) : str(new QoreString(enc)) {
00601    }
00602 
00604    DLLLOCAL TempString(QoreString *s) {
00605       str = s;
00606    }
00607 
00609    DLLLOCAL ~TempString() {
00610       delete str;
00611    }
00612 
00614    DLLLOCAL QoreString *operator->(){ return str; };
00615 
00617    DLLLOCAL QoreString *operator*() { return str; };
00618 
00620    DLLLOCAL operator bool() const { return str != 0; }
00621 
00623    DLLLOCAL QoreString *release() { QoreString *rv = str; str = 0; return rv; }
00624 };
00625 
00627 
00639 class TempEncodingHelper {
00640 private:
00641    QoreString *str;
00642    bool temp;
00643 
00645    DLLLOCAL TempEncodingHelper(const TempEncodingHelper &);
00646 
00648    DLLLOCAL TempEncodingHelper& operator=(const TempEncodingHelper &);
00649 
00651    DLLLOCAL void *operator new(size_t);
00652 
00654    DLLLOCAL void discard_intern() {
00655       if (temp && str)
00656          delete str;
00657    }
00658 
00660 
00665    DLLLOCAL void set_intern(const QoreString *s, const QoreEncoding *qe, ExceptionSink *xsink) {
00666       if (s->getEncoding() != qe) {
00667          str = s->convertEncoding(qe, xsink);
00668          temp = true;
00669       }
00670       else {
00671          str = const_cast<QoreString *>(s);
00672          temp = false;
00673       }
00674    }
00675 
00676 public:
00678 
00683    DLLLOCAL TempEncodingHelper(const QoreString &s, const QoreEncoding *qe, ExceptionSink *xsink) {
00684       set_intern(&s, qe, xsink);
00685    }
00686 
00688 
00693    DLLLOCAL TempEncodingHelper(const QoreString *s, const QoreEncoding *qe, ExceptionSink *xsink) {
00694       set_intern(s, qe, xsink);
00695    }
00696 
00698    DLLLOCAL TempEncodingHelper() : str(0), temp(false) {
00699    }
00700 
00702    DLLLOCAL ~TempEncodingHelper() {
00703       discard_intern();
00704    }
00705 
00707 
00716    DLLLOCAL int set(const QoreString *s, const QoreEncoding *qe, ExceptionSink *xsink) {
00717       discard_intern();
00718 
00719       set_intern(s, qe, xsink);
00720       return str != 0;
00721    }
00722 
00724    DLLLOCAL bool is_temp() const {
00725       return temp;
00726    }
00727 
00729    DLLLOCAL const QoreString *operator->(){ return str; };
00730 
00732    DLLLOCAL const QoreString *operator*() { return str; };
00733 
00735 
00738    DLLLOCAL operator bool() const { return str != 0; }
00739 
00741 
00744    DLLLOCAL char *giveBuffer() {
00745       if (!str)
00746          return 0;
00747       if (temp)
00748          return str->giveBuffer();
00749       return strdup(str->getBuffer());
00750    }
00751 };
00752 
00753 #endif
00754 
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines