java/String: add std::string_view constructor

This commit is contained in:
Max Kellermann 2022-07-14 13:02:58 +02:00
parent 199037c682
commit 4953a57c1c
2 changed files with 10 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -31,6 +31,12 @@
#include "util/TruncateString.hxx" #include "util/TruncateString.hxx"
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
Java::String::String(JNIEnv *_env, std::string_view _value) noexcept
// TODO: is there no way to do this without duplicating the string?
:String(_env, std::string{_value}.c_str())
{
}
char * char *
Java::String::CopyTo(JNIEnv *env, jstring value, Java::String::CopyTo(JNIEnv *env, jstring value,
char *buffer, size_t max_size) noexcept char *buffer, size_t max_size) noexcept

View File

@ -36,6 +36,7 @@
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include <string_view>
namespace Java { namespace Java {
@ -89,6 +90,8 @@ public:
String(JNIEnv *_env, const char *_value) noexcept String(JNIEnv *_env, const char *_value) noexcept
:LocalRef<jstring>(_env, _env->NewStringUTF(_value)) {} :LocalRef<jstring>(_env, _env->NewStringUTF(_value)) {}
String(JNIEnv *_env, std::string_view _value) noexcept;
static StringUTFChars GetUTFChars(JNIEnv *env, jstring s) noexcept { static StringUTFChars GetUTFChars(JNIEnv *env, jstring s) noexcept {
return {env, s, env->GetStringUTFChars(s, nullptr)}; return {env, s, env->GetStringUTFChars(s, nullptr)};
} }