CACAO
string.hpp
Go to the documentation of this file.
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
56  static JavaString from_utf8(const char*, size_t);
57 
58  static JavaString from_utf8(const char *cs) {
59  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 '.'
65  // creates a new object of type java/lang/String from a utf-text,
66  // changes '.' to '/'
68  // creates and interns a java/lang/String
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;
97 
98  /*** MISC ******************************************/
99 
100  JavaString intern() const;
101 
102  void fprint(FILE*) const;
103  void fprint_printable_ascii(FILE*) const;
104 
105  JavaString() : str(0) {}
107 
108  operator java_handle_t*() const { return str; }
109 
111  private:
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  */
const u2 * begin() const
Definition: string.cpp:350
JavaString(java_handle_t *h)
Definition: string.hpp:106
Utf8String to_utf8() const
Definition: string.cpp:437
JavaString()
Definition: string.hpp:105
JavaString intern() const
Definition: string.cpp:336
static JavaString from_utf8(Utf8String)
Definition: string.cpp:184
static JavaString from_array(java_handle_t *array, int32_t count, int32_t offset)
creates a new java/lang/String with a given char[] WARNING: the char[] is not copied or validated...
Definition: string.cpp:284
const u2 * end() const
Definition: string.cpp:369
char * to_chars() const
Definition: string.cpp:413
uint16_t u2
Definition: types.hpp:43
Simple stream class for formatted output.
Definition: OStream.hpp:141
void fprint(FILE *) const
Definition: string.cpp:466
java_handle_t * str
Definition: string.hpp:112
size_t utf8_size() const
Definition: string.cpp:394
friend cacao::OStream & operator<<(cacao::OStream &, JavaString)
Definition: string.cpp:492
static JavaString from_utf8_slash_to_dot(Utf8String)
Definition: string.cpp:202
void fprint_printable_ascii(FILE *) const
Definition: string.cpp:478
size_t size() const
Definition: string.cpp:382
static JavaString from_utf8_dot_to_slash(Utf8String)
Definition: string.cpp:216
static JavaString from_utf8(const char *cs)
Definition: string.hpp:58
static void initialize()
Definition: string.cpp:95
static JavaString literal(Utf8String)
Definition: string.cpp:257
static bool is_initialized()
Definition: string.cpp:106
static JavaString from_utf16(const u2 *, size_t)
creates a new java/lang/String from a utf16-text
Definition: string.cpp:269
Utf8String to_utf8_dot_to_slash() const
Definition: string.cpp:450