Qore Programming Language  0.9.3
QoreStringNode.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreStringNode.h
4 
5  QoreStringNode Class Definition
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2018 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QORESTRINGNODE_H
35 
36 #define _QORE_QORESTRINGNODE_H
37 
38 #include <qore/AbstractQoreNode.h>
39 #include <qore/QoreString.h>
40 
41 #include <cstring>
42 
44 
51 private:
53  DLLLOCAL QoreStringNode(QoreString* str);
54 
56  DLLLOCAL QoreStringNode& operator=(const QoreStringNode&);
57 
58  DLLLOCAL virtual bool getAsBoolImpl() const;
59  DLLLOCAL virtual int getAsIntImpl() const;
60  DLLLOCAL virtual int64 getAsBigIntImpl() const;
61  DLLLOCAL virtual double getAsFloatImpl() const;
62 
63 protected:
65  DLLEXPORT virtual ~QoreStringNode();
66 
67 public:
69  DLLEXPORT QoreStringNode();
70 
72 
76  DLLEXPORT QoreStringNode(const char* str, const QoreEncoding* enc = QCS_DEFAULT);
77 
79 
82  DLLEXPORT QoreStringNode(const QoreString& str);
83 
85 
88  DLLEXPORT QoreStringNode(const QoreStringNode& str);
89 
91 
95  DLLEXPORT QoreStringNode(const std::string& str, const QoreEncoding* enc = QCS_DEFAULT);
96 
97  // copies binary object and makes a base64-encoded string out of it
98  DLLEXPORT QoreStringNode(const BinaryNode* b);
99 
101  DLLEXPORT QoreStringNode(const BinaryNode* bin, qore_size_t maxlinelen);
102 
104 
110  DLLEXPORT QoreStringNode(char* nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding* enc);
111 
113  DLLEXPORT QoreStringNode(const char* str, qore_size_t len, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
114 
115  // creates a string from a single character
116  DLLEXPORT QoreStringNode(char c);
117 
119 
125  DLLEXPORT int getAsString(QoreString& str, int format_offset, ExceptionSink* xsink) const;
126 
128 
135  DLLEXPORT QoreString* getAsString(bool& del, int format_offset, ExceptionSink* xsink) const;
136 
138 
142  DLLEXPORT virtual QoreString* getStringRepresentation(bool& del) const;
143 
145 
148  DLLEXPORT virtual void getStringRepresentation(QoreString& str) const;
149 
151 
155  DLLEXPORT virtual DateTime* getDateTimeRepresentation(bool& del) const;
156 
158 
161  DLLEXPORT virtual void getDateTimeRepresentation(DateTime& dt) const;
162 
164  DLLEXPORT virtual AbstractQoreNode* realCopy() const;
165 
167 
171  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode* v, ExceptionSink* xsink) const;
172 
174 
179  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode* v, ExceptionSink* xsink) const;
180 
182  DLLEXPORT virtual const char* getTypeName() const;
183 
185 
190  DLLEXPORT QoreStringNode* convertEncoding(const QoreEncoding* nccs, ExceptionSink* xsink) const;
191 
193 
198  DLLEXPORT QoreStringNode* substr(qore_offset_t offset, ExceptionSink* xsink) const;
199 
201 
207  DLLEXPORT QoreStringNode* substr(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink) const;
208 
210  DLLEXPORT QoreStringNode* reverse() const;
211 
212  // copy function
213  DLLEXPORT QoreStringNode* copy() const;
214 
216  DLLEXPORT static QoreStringNode* createAndConvertEncoding(const char* str, const QoreEncoding* from, const QoreEncoding* to, ExceptionSink* xsink);
217 
219  DLLEXPORT QoreStringNode* parseBase64ToString(ExceptionSink* xsink) const;
220 
222 
227  DLLEXPORT QoreStringNode* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
228 
230  DLLEXPORT QoreStringNode* stringRefSelf() const;
231 
233  DLLLOCAL QoreStringNode(const char* str, const QoreEncoding* from, const QoreEncoding* to, ExceptionSink* xsink);
234 
236 
241  DLLEXPORT QoreStringNode* extract(qore_offset_t offset, ExceptionSink* xsink);
242 
244 
251 
253 
261 
263  DLLLOCAL QoreStringNode(struct qore_string_private* p);
264 
266  DLLLOCAL static const char* getStaticTypeName() {
267  return "string";
268  }
269 
271  DLLLOCAL static qore_type_t getStaticTypeCode() {
272  return NT_STRING;
273  }
274 
275  static const qore_type_t TYPE = NT_STRING;
276 
278  DLLLOCAL virtual void parseInit(QoreValue& val, LocalVar* oflag, int pflag, int& lvids, const QoreTypeInfo*& typeInfo);
279 };
280 
281 class QoreStringNodeMaker : public QoreStringNode {
282 private:
284  DLLLOCAL QoreStringNodeMaker(const QoreStringNodeMaker& str);
285 
287  DLLLOCAL QoreStringNodeMaker& operator=(const QoreStringNodeMaker&);
288 
289 public:
290  DLLEXPORT QoreStringNodeMaker(const char* fmt, ...);
291 };
292 
293 extern QoreStringNode* NullString;
294 
296 
303 protected:
304  QoreString* str;
305  bool del;
306 
308  DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper&); // not implemented
309 
311  DLLLOCAL QoreStringValueHelper& operator=(const QoreStringValueHelper&); // not implemented
312 
314  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
315 
317  DLLLOCAL void setup(ExceptionSink* xsink, const QoreValue n, const QoreEncoding* enc = 0);
318 
319 public:
321  DLLEXPORT QoreStringValueHelper(const QoreValue n);
322 
324 
336  DLLEXPORT QoreStringValueHelper(const QoreValue n, const QoreEncoding* enc, ExceptionSink* xsink);
337 
340  if (del)
341  delete str;
342  }
343 
345 
348  DLLLOCAL const QoreString* operator->() { return str; }
349 
351 
354  DLLLOCAL const QoreString* operator*() { return str; }
355 
357 
360  DLLLOCAL QoreString* giveString() {
361  if (!str)
362  return 0;
363  if (!del)
364  return str->copy();
365 
366  QoreString* rv = str;
367  del = false;
368  str = 0;
369  return rv;
370  }
371 
373 
375  DLLLOCAL char* giveBuffer() {
376  if (!str)
377  return 0;
378  if (!del)
379  return strdup(str->getBuffer());
380  char* rv = str->giveBuffer();
381  delete str;
382  del = false;
383  str = 0;
384  return rv;
385  }
386 
388  DLLLOCAL bool is_temp() const {
389  return del;
390  }
391 };
392 
394 
402 private:
403  QoreStringNode* str;
404  bool del;
405 
408 
410  DLLLOCAL QoreStringNodeValueHelper& operator=(const QoreStringNodeValueHelper&);
411 
413  DLLLOCAL void* operator new(size_t);
414 
416  DLLLOCAL void setup(ExceptionSink* xsink, const QoreValue n, const QoreEncoding* enc = 0);
417 
418 public:
419  DLLEXPORT QoreStringNodeValueHelper(const QoreValue n);
420 
422 
434  DLLEXPORT QoreStringNodeValueHelper(const QoreValue n, const QoreEncoding* enc, ExceptionSink* xsink);
435 
437  DLLEXPORT ~QoreStringNodeValueHelper();
438 
440 
443  DLLLOCAL const QoreStringNode* operator->() { return str; }
444 
446 
449  DLLLOCAL const QoreStringNode* operator*() { return str; }
450 
452 
456  DLLEXPORT QoreStringNode* getReferencedValue();
457 
459  DLLLOCAL bool is_temp() const {
460  return del;
461  }
462 };
463 
464 #include <qore/ReferenceHolder.h>
465 
467 
471 
472 extern QoreString NothingTypeString;
473 
475 
485 private:
486  QoreString* str;
487  bool del;
488 
490  DLLLOCAL QoreNodeAsStringHelper(const QoreNodeAsStringHelper&); // not implemented
491 
493  DLLLOCAL QoreNodeAsStringHelper& operator=(const QoreNodeAsStringHelper&); // not implemented
494 
496  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
497 
498 public:
500  DLLEXPORT QoreNodeAsStringHelper(const AbstractQoreNode* n, int format_offset, ExceptionSink* xsink);
501 
503  DLLEXPORT QoreNodeAsStringHelper(const QoreValue n, int format_offset, ExceptionSink* xsink);
504 
506  DLLEXPORT ~QoreNodeAsStringHelper();
507 
509 
512  DLLLOCAL const QoreString* operator->() { return str; }
513 
515 
518  DLLLOCAL const QoreString* operator*() { return str; }
519 
521 
524  DLLEXPORT QoreString* giveString();
525 };
526 
527 #endif
DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const
concatenates the string data in double quotes to an existing QoreString
virtual DLLEXPORT ~QoreStringNode()
destructor only called when references = 0, use deref() instead
virtual DLLLOCAL void parseInit(QoreValue &val, LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo)
returns the type information
DLLLOCAL QoreString * giveString()
returns a copy of the QoreString that the caller owns
Definition: QoreStringNode.h:360
DLLLOCAL const QoreStringNode * operator->()
returns the object being managed
Definition: QoreStringNode.h:443
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT QoreStringNode * reverse() const
return a QoreStringNode with the characters reversed
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
static DLLEXPORT QoreStringNode * createAndConvertEncoding(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink)
creates a new QoreStringNode from a string and converts its encoding
DLLLOCAL const QoreString * operator*()
returns the object being managed
Definition: QoreStringNode.h:518
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
safely manages the return values to AbstractQoreNode::getAsString(), stack only, cannot be dynamicall...
Definition: QoreStringNode.h:484
DLLEXPORT ~QoreStringNodeValueHelper()
destroys the object and dereferences the QoreStringNode if it is a temporary pointer
DLLEXPORT QoreString * giveString()
returns a copy of the QoreString that the caller owns
DLLLOCAL QoreStringValueHelper & operator=(const QoreStringValueHelper &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
DLLEXPORT char * giveBuffer()
returns the character buffer and leaves the QoreString empty, the caller owns the memory returned (mu...
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
DLLLOCAL const QoreString * operator->()
returns the object being managed
Definition: QoreStringNode.h:512
DLLLOCAL bool is_temp() const
returns true if the pointer being managed is temporary
Definition: QoreStringNode.h:388
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
DLLEXPORT QoreStringNode * getReferencedValue()
returns a referenced value - the caller will own the reference
DLLLOCAL const QoreString * operator*()
returns the object being managed
Definition: QoreStringNode.h:354
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT QoreString * copy() const
returns an exact copy of the string
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
DLLEXPORT QoreStringNode * substr(qore_offset_t offset, ExceptionSink *xsink) const
returns a new string consisting of all the characters from the current string starting with character...
DLLLOCAL char * giveBuffer()
returns a char* string that the caller owns and must free()
Definition: QoreStringNode.h:375
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a simpl...
Definition: QoreStringNode.h:302
DLLEXPORT QoreStringNode()
creates an empty string and assigns the default encoding QCS_DEFAULT
DLLEXPORT QoreStringNode * convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const
converts the encoding of the string to the specified encoding, returns 0 if an error occurs,...
DLLLOCAL void setup(ExceptionSink *xsink, const QoreValue n, const QoreEncoding *enc=0)
sets up the object / common initialization
DLLLOCAL ~QoreStringValueHelper()
destroys the object and deletes the QoreString pointer being managed if it was a temporary pointer
Definition: QoreStringNode.h:339
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
DLLEXPORT QoreStringNode * extract(qore_offset_t offset, ExceptionSink *xsink)
removes characters from the string starting at position "offset" and returns a string of the characte...
DLLLOCAL const QoreString * operator->()
returns the object being managed
Definition: QoreStringNode.h:348
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
virtual DLLEXPORT bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values for container types) without type o...
virtual DLLEXPORT QoreString * getStringRepresentation(bool &del) const
returns the current string and sets del to false
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:76
DLLEXPORT ~QoreNodeAsStringHelper()
destroys the object and deletes the QoreString pointer being managed if it was a temporary pointer
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a QoreS...
Definition: QoreStringNode.h:401
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: QoreStringNode.h:271
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition: common.h:70
DLLLOCAL const QoreStringNode * operator*()
returns the object being managed
Definition: QoreStringNode.h:449
DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
base class for simple value types
Definition: AbstractQoreNode.h:366
virtual DLLEXPORT bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const
tests for equality ("deep compare" including all contained values for container types) with possible ...
virtual DLLEXPORT DateTime * getDateTimeRepresentation(bool &del) const
returns the DateTime representation of this string
DLLEXPORT QoreStringNode * parseBase64ToString(ExceptionSink *xsink) const
parses the string as a base64-encoded binary and returns the decoded value as a QoreStringNode
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: QoreStringNode.h:266
virtual DLLEXPORT AbstractQoreNode * realCopy() const
returns a copy of the object, the caller owns the reference count
DLLLOCAL bool is_temp() const
returns true if the referenced being managed is temporary
Definition: QoreStringNode.h:459
DLLEXPORT QoreString & operator=(const QoreString &other)
assigns the value of one string to another
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT qore_size_t length() const
returns the number of characters (not bytes) in the string