as2js: /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/string.h Source File

string.h
Go to the documentation of this file.
1 #ifndef AS2JS_STRING_H
2 #define AS2JS_STRING_H
3 /* include/as2js/string.h
4 
5 Copyright (c) 2005-2019 Made to Order Software Corp. All Rights Reserved
6 
8 
9 Permission is hereby granted, free of charge, to any
10 person obtaining a copy of this software and
11 associated documentation files (the "Software"), to
12 deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify,
14 merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom
16 the Software is furnished to do so, subject to the
17 following conditions:
18 
19 The above copyright notice and this permission notice
20 shall be included in all copies or substantial
21 portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
24 ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
25 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
26 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
27 EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 SOFTWARE.
33 
34 */
35 
36 #include "int64.h"
37 #include "float64.h"
38 
39 #include <iostream>
40 #include <string>
41 
42 namespace as2js
43 {
44 
45 // our character type, yes, it also becomes as String::value_type
46 // but at least this way we control the size outside of the class
47 typedef int32_t as_char_t;
48 
49 // Our String type is a UCS-4 compatible string type
50 // Unfortunately, under MS-Windows wstring is 16 bits
51 class String
52  : public std::basic_string<as_char_t>
53 {
54 public:
55  // Unicode BOM character
56  static as_char_t const STRING_BOM = 0xFEFF;
57  // Represents a continuation character (i.e. '\' + LineTerminatorSequence)
58  static as_char_t const STRING_CONTINUATION = -2;
59 
61  {
62  STRING_GOOD = 0, // string conversion succeeded
63  STRING_END = -1, // not enough data to form a character
64  STRING_BAD = -2, // input is not valid (bad encoding sequence)
65  STRING_INVALID = -3 // invalid character found (character is not between 0 and 0x10FFFF, or is a code point reserved for UTF-16 surrogates)
66  };
67 
68  String();
69  String(char const * str, int len = -1);
70  String(wchar_t const * str, int len = -1);
71  String(as_char_t const * str, int len = -1);
72  String(std::string const & str);
73  String(std::wstring const & str);
74  String(std::basic_string<as_char_t> const & str);
75 
76  String& operator = (char const * str);
77  String& operator = (wchar_t const * str);
78  String& operator = (as_char_t const * str);
79  String& operator = (std::string const & str);
80  String& operator = (std::wstring const & str);
81  String& operator = (std::basic_string<as_char_t> const & str);
82 
83  bool operator == (char const * str) const;
84  friend bool operator == (char const * str, String const & string);
85  bool operator != (char const * str) const;
86  friend bool operator != (char const * str, String const & string);
87 
88  String & operator += (char const * str);
89  String & operator += (wchar_t const * str);
90  String & operator += (as_char_t const * str);
91  String & operator += (std::string const & str);
92  String & operator += (std::wstring const & str);
93  String & operator += (std::basic_string<as_char_t> const & str);
94 
95  String & operator += (as_char_t const c);
96  String & operator += (char const c);
97  String & operator += (wchar_t const c);
98 
99  //String operator + (char const * str);
100  //String operator + (wchar_t const * str);
101  //String operator + (as_char_t const * str);
102  //String operator + (std::string const & str);
103  //String operator + (std::wstring const & str);
104 
105  bool valid() const;
106  static bool valid_character(as_char_t c);
107 
108  bool is_int64() const;
109  bool is_float64() const;
110  bool is_number() const;
111  Int64::int64_type to_int64() const;
113  bool is_true() const;
114 
115  conversion_result_t from_char(char const * str, int len = -1);
116  conversion_result_t from_wchar(wchar_t const * str, int len = -1);
117  conversion_result_t from_as_char(as_char_t const * str, int len = -1);
118  conversion_result_t from_utf8(char const * str, int len = -1);
119  conversion_result_t from_utf8(std::string const & str);
120 
121  ssize_t utf8_length() const;
122  std::string to_utf8() const;
123 
124  String simplified() const;
125 };
126 
127 std::ostream& operator << (std::ostream& out, String const& str);
128 
129 
130 }
131 // namespace as2js
132 
133 #endif
134 // #ifndef AS2JS_STRING_H
135 
136 // vim: ts=4 sw=4 et
bool operator!=(char const *str) const
Compare this String against a char const * string.
Definition: string.cpp:942
int32_t as_char_t
Definition: string.h:47
bool valid() const
Check validity of the string.
Definition: string.cpp:986
String simplified() const
Make a simplified copy of this string.
Definition: string.cpp:1400
double float64_type
Definition: float64.h:48
ssize_t utf8_length() const
Calculate the length if converted to UTF-8.
Definition: string.cpp:1283
std::string to_utf8() const
Convert a string to UTF-8 and return the result.
Definition: string.cpp:1343
Float64::float64_type to_float64() const
Convert a string to a floating point number.
Definition: string.cpp:1230
String & operator+=(char const *str)
Append str to this String.
Definition: string.cpp:309
String()
Initialize an empty string.
Definition: string.cpp:60
bool is_int64() const
Check whether this string represents a valid integer.
Definition: string.cpp:1037
bool is_number() const
Check whether this string represents a number.
Definition: string.cpp:1155
conversion_result_t
Definition: string.h:60
String & operator=(char const *str)
Copy str in this String.
Definition: string.cpp:218
bool is_true() const
Check whether the string is considered true.
Definition: string.cpp:1253
conversion_result_t from_as_char(as_char_t const *str, int len=-1)
Copy an as_char_t string to this String.
Definition: string.cpp:733
Int64::int64_type to_int64() const
Convert a string to an integer number.
Definition: string.cpp:1181
static as_char_t const STRING_BOM
Definition: string.h:56
static as_char_t const STRING_CONTINUATION
Definition: string.h:58
conversion_result_t from_utf8(char const *str, int len=-1)
Copy a UTF-8 string to this String.
Definition: string.cpp:792
std::ostream & operator<<(std::ostream &out, Node const &node)
Send a node to the specified output stream.
bool operator==(char const *str) const
Compare this String against a char const * string.
Definition: string.cpp:907
conversion_result_t from_wchar(wchar_t const *str, int len=-1)
Copy a wchar_t string to this String.
Definition: string.cpp:638
The AlexScript to JavaScript namespace.
Definition: compiler.cpp:37
bool is_float64() const
Check whether the string represents a valid floating pointer number.
Definition: string.cpp:1105
conversion_result_t from_char(char const *str, int len=-1)
Concatenate a String and a C-string.
Definition: string.cpp:588
int64_t int64_type
Definition: int64.h:47
static bool valid_character(as_char_t c)
Check whether a character is considered valid.
Definition: string.cpp:1015

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly