Qore Programming Language  0.8.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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_QORESTRINGNODE_H
27 
28 #define _QORE_QORESTRINGNODE_H
29 
30 #include <qore/AbstractQoreNode.h>
31 #include <qore/QoreString.h>
32 
34 
41 private:
43  DLLLOCAL QoreStringNode(QoreString *str);
44 
46  DLLLOCAL QoreStringNode& operator=(const QoreStringNode&);
47 
48  DLLLOCAL virtual bool getAsBoolImpl() const;
49  DLLLOCAL virtual int getAsIntImpl() const;
50  DLLLOCAL virtual int64 getAsBigIntImpl() const;
51  DLLLOCAL virtual double getAsFloatImpl() const;
52 
53 protected:
55  DLLEXPORT virtual ~QoreStringNode();
56 
57 public:
59  DLLEXPORT QoreStringNode();
60 
62 
66  DLLEXPORT QoreStringNode(const char *str, const QoreEncoding *enc = QCS_DEFAULT);
67 
69 
72  DLLEXPORT QoreStringNode(const QoreString &str);
73 
75 
78  DLLEXPORT QoreStringNode(const QoreStringNode &str);
79 
81 
85  DLLEXPORT QoreStringNode(const std::string &str, const QoreEncoding *enc = QCS_DEFAULT);
86 
87  // copies binary object and makes a base64-encoded string out of it
88  DLLEXPORT QoreStringNode(const BinaryNode *b);
89 
91  DLLEXPORT QoreStringNode(const BinaryNode *bin, qore_size_t maxlinelen);
92 
94 
100  DLLEXPORT QoreStringNode(char *nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding *enc);
101 
103  DLLEXPORT QoreStringNode(const char *str, qore_size_t len, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
104 
105  // creates a string from a single character
106  DLLEXPORT QoreStringNode(char c);
107 
109 
115  DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const;
116 
118 
125  DLLEXPORT QoreString *getAsString(bool &del, int format_offset, ExceptionSink *xsink) const;
126 
128 
132  DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
133 
135 
138  DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
139 
141 
145  DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
146 
148 
151  DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
152 
154  DLLEXPORT virtual AbstractQoreNode *realCopy() const;
155 
157 
161  DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
162 
164 
169  DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
170 
172  DLLEXPORT virtual const char *getTypeName() const;
173 
175 
180  DLLEXPORT QoreStringNode *convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const;
181 
183 
188  DLLEXPORT QoreStringNode *substr(qore_offset_t offset, ExceptionSink *xsink) const;
189 
191 
197  DLLEXPORT QoreStringNode *substr(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
198 
200  DLLEXPORT QoreStringNode *reverse() const;
201 
202  // copy function
203  DLLEXPORT QoreStringNode *copy() const;
204 
206  DLLEXPORT static QoreStringNode *createAndConvertEncoding(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
207 
209  DLLEXPORT QoreStringNode *parseBase64ToString(ExceptionSink *xsink) const;
210 
212 
217  DLLEXPORT QoreStringNode* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
218 
220  DLLEXPORT QoreStringNode *stringRefSelf() const;
221 
223  DLLLOCAL QoreStringNode(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
224 
226 
231  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, ExceptionSink *xsink);
232 
234 
240  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
241 
243 
250  DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
251 
253  DLLLOCAL QoreStringNode(struct qore_string_private *p);
254 
256  DLLLOCAL static const char *getStaticTypeName() {
257  return "string";
258  }
259 
261  DLLLOCAL static qore_type_t getStaticTypeCode() {
262  return NT_STRING;
263  }
264 
266  DLLLOCAL virtual AbstractQoreNode *parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
267 };
268 
269 class QoreStringNodeMaker : public QoreStringNode {
270 private:
272  DLLLOCAL QoreStringNodeMaker(const QoreStringNodeMaker& str);
273 
275  DLLLOCAL QoreStringNodeMaker& operator=(const QoreStringNodeMaker&);
276 
277 public:
278  DLLEXPORT QoreStringNodeMaker(const char* fmt, ...);
279 };
280 
281 extern QoreStringNode *NullString;
282 
284 
291 private:
292  QoreString *str;
293  bool del;
294 
296  DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper&); // not implemented
297 
299  DLLLOCAL QoreStringValueHelper& operator=(const QoreStringValueHelper&); // not implemented
300 
302  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
303 
304 public:
307  if (n) {
308  //optimization to remove the need for a virtual function call in the most common case
309  if (n->getType() == NT_STRING) {
310  del = false;
311  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
312  }
313  else
314  str = n->getStringRepresentation(del);
315  }
316  else {
317  str = NullString;
318  del = false;
319  }
320  }
321 
323 
335  DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n, const QoreEncoding *enc, ExceptionSink *xsink) {
336  if (n) {
337  //optimization to remove the need for a virtual function call in the most common case
338  if (n->getType() == NT_STRING) {
339  del = false;
340  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
341  }
342  else
343  str = n->getStringRepresentation(del);
344  if (str->getEncoding() != enc) {
345  QoreString *t = str->convertEncoding(enc, xsink);
346  if (!t)
347  return;
348  if (del)
349  delete str;
350  str = t;
351  del = true;
352  }
353  }
354  else {
355  str = NullString;
356  del = false;
357  }
358  }
359 
362  if (del)
363  delete str;
364  }
365 
367 
370  DLLLOCAL const QoreString *operator->() { return str; }
371 
373 
376  DLLLOCAL const QoreString *operator*() { return str; }
377 
379 
382  DLLLOCAL QoreString *giveString() {
383  if (!str)
384  return 0;
385  if (!del)
386  return str->copy();
387 
388  QoreString *rv = str;
389  del = false;
390  str = 0;
391  return rv;
392  }
393 
395  DLLLOCAL bool is_temp() const {
396  return del;
397  }
398 };
399 
401 
409 private:
410  QoreStringNode *str;
411  bool temp;
412 
415 
417  DLLLOCAL QoreStringNodeValueHelper& operator=(const QoreStringNodeValueHelper&);
418 
420  DLLLOCAL void* operator new(size_t);
421 
422 public:
423  DLLLOCAL QoreStringNodeValueHelper(const AbstractQoreNode *n) {
424  if (!n) {
425  str = NullString;
426  temp = false;
427  return;
428  }
429 
430  qore_type_t ntype = n->getType();
431  if (ntype == NT_STRING) {
432  str = const_cast<QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
433  temp = false;
434  }
435  else {
436  str = new QoreStringNode();
437  n->getStringRepresentation(*(static_cast<QoreString *>(str)));
438  temp = true;
439  }
440  }
441 
444  if (temp)
445  str->deref();
446  }
447 
449 
452  DLLLOCAL const QoreStringNode *operator->() { return str; }
453 
455 
458  DLLLOCAL const QoreStringNode *operator*() { return str; }
459 
461 
466  if (temp)
467  temp = false;
468  else if (str)
469  str->ref();
470  return str;
471  }
472 };
473 
474 #include <qore/ReferenceHolder.h>
475 
477 
481 
482 extern QoreString NothingTypeString;
483 
485 
495 private:
496  QoreString *str;
497  bool del;
498 
500  DLLLOCAL QoreNodeAsStringHelper(const QoreNodeAsStringHelper&); // not implemented
501 
503  DLLLOCAL QoreNodeAsStringHelper& operator=(const QoreNodeAsStringHelper&); // not implemented
504 
506  DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
507 
508 public:
510  DLLEXPORT QoreNodeAsStringHelper(const AbstractQoreNode *n, int format_offset, ExceptionSink *xsink);
511 
514  if (del)
515  delete str;
516  }
517 
519 
522  DLLLOCAL const QoreString *operator->() { return str; }
523 
525 
528  DLLLOCAL const QoreString *operator*() { return str; }
529 
531 
534  DLLLOCAL QoreString *giveString() {
535  if (!str)
536  return 0;
537  if (!del)
538  return str->copy();
539 
540  QoreString *rv = str;
541  del = false;
542  str = 0;
543  return rv;
544  }
545 };
546 
547 #endif