Qore Programming Language  0.8.11.1
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 - 2014 David Nichols
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 
42 
49 private:
51  DLLLOCAL QoreStringNode(QoreString *str);
52 
54  DLLLOCAL QoreStringNode& operator=(const QoreStringNode&);
55 
56  DLLLOCAL virtual bool getAsBoolImpl() const;
57  DLLLOCAL virtual int getAsIntImpl() const;
58  DLLLOCAL virtual int64 getAsBigIntImpl() const;
59  DLLLOCAL virtual double getAsFloatImpl() const;
60 
61 protected:
63  DLLEXPORT virtual ~QoreStringNode();
64 
65 public:
67  DLLEXPORT QoreStringNode();
68 
70 
74  DLLEXPORT QoreStringNode(const char *str, const QoreEncoding *enc = QCS_DEFAULT);
75 
77 
80  DLLEXPORT QoreStringNode(const QoreString &str);
81 
83 
86  DLLEXPORT QoreStringNode(const QoreStringNode &str);
87 
89 
93  DLLEXPORT QoreStringNode(const std::string &str, const QoreEncoding *enc = QCS_DEFAULT);
94 
95  // copies binary object and makes a base64-encoded string out of it
96  DLLEXPORT QoreStringNode(const BinaryNode *b);
97 
99  DLLEXPORT QoreStringNode(const BinaryNode *bin, qore_size_t maxlinelen);
100 
102 
108  DLLEXPORT QoreStringNode(char *nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding *enc);
109 
111  DLLEXPORT QoreStringNode(const char *str, qore_size_t len, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
112 
113  // creates a string from a single character
114  DLLEXPORT QoreStringNode(char c);
115 
117 
123  DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const;
124 
126 
133  DLLEXPORT QoreString *getAsString(bool &del, int format_offset, ExceptionSink *xsink) const;
134 
136 
140  DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
141 
143 
146  DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
147 
149 
153  DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
154 
156 
159  DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
160 
162  DLLEXPORT virtual AbstractQoreNode *realCopy() const;
163 
165 
169  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
170 
172 
177  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
178 
180  DLLEXPORT virtual const char *getTypeName() const;
181 
183 
188  DLLEXPORT QoreStringNode *convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const;
189 
191 
196  DLLEXPORT QoreStringNode *substr(qore_offset_t offset, ExceptionSink *xsink) const;
197 
199 
205  DLLEXPORT QoreStringNode *substr(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
206 
208  DLLEXPORT QoreStringNode *reverse() const;
209 
210  // copy function
211  DLLEXPORT QoreStringNode *copy() const;
212 
214  DLLEXPORT static QoreStringNode *createAndConvertEncoding(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
215 
217  DLLEXPORT QoreStringNode *parseBase64ToString(ExceptionSink *xsink) const;
218 
220 
225  DLLEXPORT QoreStringNode* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
226 
228  DLLEXPORT QoreStringNode *stringRefSelf() const;
229 
231  DLLLOCAL QoreStringNode(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
232 
234 
239  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, ExceptionSink *xsink);
240 
242 
248  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
249 
251 
258  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
259 
261  DLLLOCAL QoreStringNode(struct qore_string_private *p);
262 
264  DLLLOCAL static const char *getStaticTypeName() {
265  return "string";
266  }
267 
269  DLLLOCAL static qore_type_t getStaticTypeCode() {
270  return NT_STRING;
271  }
272 
274  DLLLOCAL virtual AbstractQoreNode *parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
275 };
276 
277 class QoreStringNodeMaker : public QoreStringNode {
278 private:
280  DLLLOCAL QoreStringNodeMaker(const QoreStringNodeMaker& str);
281 
283  DLLLOCAL QoreStringNodeMaker& operator=(const QoreStringNodeMaker&);
284 
285 public:
286  DLLEXPORT QoreStringNodeMaker(const char* fmt, ...);
287 };
288 
289 extern QoreStringNode *NullString;
290 
292 
299 private:
300  QoreString *str;
301  bool del;
302 
304  DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper&); // not implemented
305 
307  DLLLOCAL QoreStringValueHelper& operator=(const QoreStringValueHelper&); // not implemented
308 
310  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
311 
312 public:
315  if (n) {
316  //optimization to remove the need for a virtual function call in the most common case
317  if (n->getType() == NT_STRING) {
318  del = false;
319  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
320  }
321  else
322  str = n->getStringRepresentation(del);
323  }
324  else {
325  str = NullString;
326  del = false;
327  }
328  }
329 
331 
343  DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n, const QoreEncoding *enc, ExceptionSink *xsink) {
344  if (n) {
345  //optimization to remove the need for a virtual function call in the most common case
346  if (n->getType() == NT_STRING) {
347  del = false;
348  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
349  }
350  else
351  str = n->getStringRepresentation(del);
352  if (str->getEncoding() != enc) {
353  QoreString *t = str->convertEncoding(enc, xsink);
354  if (!t)
355  return;
356  if (del)
357  delete str;
358  str = t;
359  del = true;
360  }
361  }
362  else {
363  str = NullString;
364  del = false;
365  }
366  }
367 
370  if (del)
371  delete str;
372  }
373 
375 
378  DLLLOCAL const QoreString *operator->() { return str; }
379 
381 
384  DLLLOCAL const QoreString *operator*() { return str; }
385 
387 
390  DLLLOCAL QoreString *giveString() {
391  if (!str)
392  return 0;
393  if (!del)
394  return str->copy();
395 
396  QoreString *rv = str;
397  del = false;
398  str = 0;
399  return rv;
400  }
401 
403  DLLLOCAL bool is_temp() const {
404  return del;
405  }
406 };
407 
409 
417 private:
418  QoreStringNode *str;
419  bool temp;
420 
423 
425  DLLLOCAL QoreStringNodeValueHelper& operator=(const QoreStringNodeValueHelper&);
426 
428  DLLLOCAL void* operator new(size_t);
429 
430 public:
431  DLLLOCAL QoreStringNodeValueHelper(const AbstractQoreNode *n) {
432  if (!n) {
433  str = NullString;
434  temp = false;
435  return;
436  }
437 
438  qore_type_t ntype = n->getType();
439  if (ntype == NT_STRING) {
440  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
441  temp = false;
442  }
443  else {
444  str = new QoreStringNode();
445  n->getStringRepresentation(*(static_cast<QoreString *>(str)));
446  temp = true;
447  }
448  }
449 
452  if (temp)
453  str->deref();
454  }
455 
457 
460  DLLLOCAL const QoreStringNode *operator->() { return str; }
461 
463 
466  DLLLOCAL const QoreStringNode *operator*() { return str; }
467 
469 
474  if (temp)
475  temp = false;
476  else if (str)
477  str->ref();
478  return str;
479  }
480 };
481 
482 #include <qore/ReferenceHolder.h>
483 
485 
489 
490 extern QoreString NothingTypeString;
491 
493 
503 private:
504  QoreString *str;
505  bool del;
506 
508  DLLLOCAL QoreNodeAsStringHelper(const QoreNodeAsStringHelper&); // not implemented
509 
511  DLLLOCAL QoreNodeAsStringHelper& operator=(const QoreNodeAsStringHelper&); // not implemented
512 
514  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
515 
516 public:
518  DLLEXPORT QoreNodeAsStringHelper(const AbstractQoreNode *n, int format_offset, ExceptionSink *xsink);
519 
522  if (del)
523  delete str;
524  }
525 
527 
530  DLLLOCAL const QoreString *operator->() { return str; }
531 
533 
536  DLLLOCAL const QoreString *operator*() { return str; }
537 
539 
542  DLLLOCAL QoreString *giveString() {
543  if (!str)
544  return 0;
545  if (!del)
546  return str->copy();
547 
548  QoreString *rv = str;
549  del = false;
550  str = 0;
551  return rv;
552  }
553 };
554 
555 #endif
virtual DLLEXPORT ~QoreStringNode()
destructor only called when references = 0, use deref() instead
DLLLOCAL QoreString * giveString()
returns a copy of the QoreString that the caller owns
Definition: QoreStringNode.h:390
DLLLOCAL const QoreStringNode * operator->()
returns the object being managed
Definition: QoreStringNode.h:460
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...
defines string encoding functions in Qore
Definition: QoreEncoding.h:80
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:536
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:502
DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n, const QoreEncoding *enc, ExceptionSink *xsink)
gets the QoreString representation and ensures that it's in the desired encoding
Definition: QoreStringNode.h:343
Holds absolute and relative date/time values in Qore with precision to the microsecond.
Definition: DateTime.h:93
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:55
DLLLOCAL const QoreString * operator->()
returns the object being managed
Definition: QoreStringNode.h:530
DLLEXPORT QoreStringNode * reverse() const
return a QoreStringNode with the characters reversed
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:70
virtual DLLEXPORT QoreString * getStringRepresentation(bool &del) const
returns the value of the type converted to a string, default implementation: returns the empty string...
long qore_offset_t
used for offsets that could be negative
Definition: common.h:73
DLLLOCAL const QoreString * operator*()
returns the object being managed
Definition: QoreStringNode.h:384
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 QoreString * getStringRepresentation(bool &del) const
returns the current string and sets del to false
DLLLOCAL bool is_temp() const
returns true if the pointer being managed is temporary
Definition: QoreStringNode.h:403
DLLLOCAL QoreStringNode * getReferencedValue()
returns a referenced value - the caller will own the reference
Definition: QoreStringNode.h:473
signed short qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode) ...
Definition: common.h:67
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:50
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:48
DLLEXPORT QoreString * convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const
converts the encoding of the string to the specified encoding, returns 0 if an error occurs...
DLLLOCAL ~QoreStringNodeValueHelper()
destroys the object and dereferences the QoreStringNode if it is a temporary pointer ...
Definition: QoreStringNode.h:451
DLLEXPORT qore_size_t length() const
returns the number of characters (not bytes) in the string
virtual DLLEXPORT const char * getTypeName() const
returns the type name as a c string
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...
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a simpl...
Definition: QoreStringNode.h:298
DLLEXPORT QoreStringNode * parseBase64ToString(ExceptionSink *xsink) const
parses the string as a base64-encoded binary and returns the decoded value as a QoreStringNode ...
DLLEXPORT QoreStringNode()
creates an empty string and assigns the default encoding QCS_DEFAULT
virtual DLLEXPORT DateTime * getDateTimeRepresentation(bool &del) const
returns the DateTime representation of this string
DLLLOCAL ~QoreStringValueHelper()
destroys the object and deletes the QoreString pointer being managed if it was a temporary pointer ...
Definition: QoreStringNode.h:369
DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n)
creates the object and acquires a pointer to the QoreString representation of the AbstractQoreNode pa...
Definition: QoreStringNode.h:314
DLLEXPORT const QoreEncoding * getEncoding() const
returns the encoding for the string
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
DLLEXPORT void ref() const
increments the reference count
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:378
DLLEXPORT QoreString * copy() const
returns an exact copy of the string
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:43
DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const
concatenates the string data in double quotes to an existing QoreString
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:111
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
virtual DLLLOCAL AbstractQoreNode * parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo)
returns the type information
this class is used to safely manage calls to AbstractQoreNode::getStringRepresentation() when a QoreS...
Definition: QoreStringNode.h:416
static DLLLOCAL qore_type_t getStaticTypeCode()
returns the type code (useful in templates)
Definition: QoreStringNode.h:269
DLLLOCAL qore_type_t getType() const
returns the data type
Definition: AbstractQoreNode.h:294
DLLLOCAL QoreString * giveString()
returns a copy of the QoreString that the caller owns
Definition: QoreStringNode.h:542
DLLLOCAL const QoreStringNode * operator*()
returns the object being managed
Definition: QoreStringNode.h:466
DLLLOCAL ~QoreNodeAsStringHelper()
destroys the object and deletes the QoreString pointer being managed if it was a temporary pointer ...
Definition: QoreStringNode.h:521
base class for simple value types
Definition: AbstractQoreNode.h:425
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 AbstractQoreNode * realCopy() const
returns a copy of the object, the caller owns the reference count
static DLLLOCAL const char * getStaticTypeName()
returns the type name (useful in templates)
Definition: QoreStringNode.h:264
DLLEXPORT QoreString & operator=(const QoreString &other)
assigns the value of one string to another
holds arbitrary binary data
Definition: BinaryNode.h:41