Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
9e61bda5
Commit
9e61bda5
authored
Jul 22, 2021
by
Max Kellermann
Committed by
Max Kellermann
Aug 27, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
java/String: add class StringUTFChars()
parent
56997290
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
16 deletions
+51
-16
String.cxx
src/java/String.cxx
+3
-16
String.hxx
src/java/String.hxx
+48
-0
No files found.
src/java/String.cxx
View file @
9e61bda5
...
...
@@ -35,13 +35,8 @@ char *
Java
::
String
::
CopyTo
(
JNIEnv
*
env
,
jstring
value
,
char
*
buffer
,
size_t
max_size
)
noexcept
{
const
char
*
p
=
env
->
GetStringUTFChars
(
value
,
nullptr
);
if
(
p
==
nullptr
)
return
nullptr
;
char
*
result
=
CopyTruncateString
(
buffer
,
p
,
max_size
);
env
->
ReleaseStringUTFChars
(
value
,
p
);
return
result
;
return
CopyTruncateString
(
buffer
,
GetUTFChars
(
env
,
value
).
c_str
(),
max_size
);
}
std
::
string
...
...
@@ -50,13 +45,5 @@ Java::String::ToString(JNIEnv *env, jstring s) noexcept
assert
(
env
!=
nullptr
);
assert
(
s
!=
nullptr
);
const
char
*
p
=
env
->
GetStringUTFChars
(
s
,
nullptr
);
if
(
p
==
nullptr
)
return
std
::
string
();
AtScopeExit
(
env
,
s
,
p
)
{
env
->
ReleaseStringUTFChars
(
s
,
p
);
};
return
std
::
string
(
p
);
return
std
::
string
(
GetUTFChars
(
env
,
s
).
c_str
());
}
src/java/String.hxx
View file @
9e61bda5
...
...
@@ -39,6 +39,46 @@
namespace
Java
{
class
StringUTFChars
{
JNIEnv
*
env
;
jstring
string
;
const
char
*
chars
=
nullptr
;
public
:
StringUTFChars
()
noexcept
=
default
;
StringUTFChars
(
std
::
nullptr_t
)
noexcept
{}
StringUTFChars
(
JNIEnv
*
_env
,
const
jstring
_string
,
const
char
*
const
_chars
)
noexcept
:
env
(
_env
),
string
(
_string
),
chars
(
_chars
)
{}
StringUTFChars
(
StringUTFChars
&&
src
)
noexcept
:
env
(
src
.
env
),
string
(
src
.
string
),
chars
(
std
::
exchange
(
src
.
chars
,
nullptr
))
{}
~
StringUTFChars
()
noexcept
{
if
(
chars
!=
nullptr
)
env
->
ReleaseStringUTFChars
(
string
,
chars
);
}
StringUTFChars
&
operator
=
(
StringUTFChars
&&
src
)
noexcept
{
using
std
::
swap
;
swap
(
env
,
src
.
env
);
swap
(
string
,
src
.
string
);
swap
(
chars
,
src
.
chars
);
return
*
this
;
}
const
char
*
c_str
()
const
noexcept
{
return
chars
;
}
operator
bool
()
const
noexcept
{
return
chars
!=
nullptr
;
}
};
/**
* Wrapper for a local "jstring" reference.
*/
...
...
@@ -49,6 +89,14 @@ public:
String
(
JNIEnv
*
_env
,
const
char
*
_value
)
noexcept
:
LocalRef
<
jstring
>
(
_env
,
_env
->
NewStringUTF
(
_value
))
{}
static
StringUTFChars
GetUTFChars
(
JNIEnv
*
env
,
jstring
s
)
noexcept
{
return
{
env
,
s
,
env
->
GetStringUTFChars
(
s
,
nullptr
)};
}
StringUTFChars
GetUTFChars
()
const
noexcept
{
return
GetUTFChars
(
GetEnv
(),
Get
());
}
/**
* Copy the value to the specified buffer. Truncates
* the value if it does not fit into the buffer.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment