Line data Source code
1 : /* src/vm/string.hpp - string header
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #ifndef STRING_HPP_
27 : #define STRING_HPP_ 1
28 :
29 : #include "config.h"
30 :
31 : #include "vm/types.hpp"
32 : #include "vm/global.hpp"
33 : #include "vm/utf8.hpp"
34 :
35 : #include <cstdio>
36 : #include <cstring>
37 :
38 : namespace cacao {
39 : class OStream;
40 : }
41 :
42 : class JavaString {
43 : public:
44 : /*** GLOBAL INITIALIZATION **********************************/
45 :
46 : // initialize string subsystem
47 : static void initialize();
48 :
49 : // check if string subsystem is initialized
50 : static bool is_initialized();
51 :
52 : /*** CONSTRUCTORS ******************************************/
53 :
54 : // creates a new java/lang/String from a utf-text
55 : static JavaString from_utf8(Utf8String);
56 : static JavaString from_utf8(const char*, size_t);
57 :
58 13699 : static JavaString from_utf8(const char *cs) {
59 13699 : return from_utf8(cs, std::strlen(cs));
60 : }
61 :
62 : // creates a new object of type java/lang/String from a utf-text,
63 : // changes '/' to '.'
64 : static JavaString from_utf8_slash_to_dot(Utf8String);
65 : // creates a new object of type java/lang/String from a utf-text,
66 : // changes '.' to '/'
67 : static JavaString from_utf8_dot_to_slash(Utf8String);
68 : // creates and interns a java/lang/String
69 : static JavaString literal(Utf8String);
70 :
71 : /// creates a new java/lang/String from a utf16-text
72 : static JavaString from_utf16(const u2*, size_t);
73 :
74 : #ifdef WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH
75 : /// creates a new java/lang/String with a given char[]
76 : /// WARNING: the char[] is not copied or validated,
77 : /// you must make sure it is never changed.
78 : static JavaString from_array(java_handle_t *array, int32_t count, int32_t offset);
79 : #endif
80 :
81 : /*** ACCESSORS ******************************************/
82 :
83 : const u2* begin() const;
84 : const u2* end() const;
85 :
86 : size_t size() const;
87 :
88 : // the number of bytes this string would need
89 : // in utf-8 encoding
90 : size_t utf8_size() const;
91 :
92 : /*** CONVERSIONS ******************************************/
93 :
94 : char* to_chars() const; // you must free the char* yourself
95 : Utf8String to_utf8() const;
96 : Utf8String to_utf8_dot_to_slash() const;
97 :
98 : /*** MISC ******************************************/
99 :
100 : JavaString intern() const;
101 :
102 : void fprint(FILE*) const;
103 : void fprint_printable_ascii(FILE*) const;
104 :
105 1009 : JavaString() : str(0) {}
106 777194 : JavaString(java_handle_t *h) : str(h) {}
107 :
108 419048 : operator java_handle_t*() const { return str; }
109 :
110 : friend cacao::OStream& operator<<(cacao::OStream&, JavaString);
111 : private:
112 : java_handle_t *str;
113 : };
114 :
115 : #endif // STRING_HPP_
116 :
117 : /*
118 : * These are local overrides for various environment variables in Emacs.
119 : * Please do not remove this and leave it at the end of the file, where
120 : * Emacs will automagically detect them.
121 : * ---------------------------------------------------------------------
122 : * Local variables:
123 : * mode: c++
124 : * indent-tabs-mode: t
125 : * c-basic-offset: 4
126 : * tab-width: 4
127 : * End:
128 : * vim:noexpandtab:sw=4:ts=4:
129 : */
|