From 4953a57c1ce244d44e2968a32708b252ae923fb1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 14 Jul 2022 13:02:58 +0200 Subject: [PATCH] java/String: add std::string_view constructor --- src/java/String.cxx | 8 +++++++- src/java/String.hxx | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/java/String.cxx b/src/java/String.cxx index 71a99f177..2cd61e22c 100644 --- a/src/java/String.cxx +++ b/src/java/String.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 Max Kellermann + * Copyright 2010-2021 Max Kellermann * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -31,6 +31,12 @@ #include "util/TruncateString.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 * Java::String::CopyTo(JNIEnv *env, jstring value, char *buffer, size_t max_size) noexcept diff --git a/src/java/String.hxx b/src/java/String.hxx index f60e7abc7..34df72feb 100644 --- a/src/java/String.hxx +++ b/src/java/String.hxx @@ -36,6 +36,7 @@ #include #include +#include namespace Java { @@ -89,6 +90,8 @@ public: String(JNIEnv *_env, const char *_value) noexcept :LocalRef(_env, _env->NewStringUTF(_value)) {} + String(JNIEnv *_env, std::string_view _value) noexcept; + static StringUTFChars GetUTFChars(JNIEnv *env, jstring s) noexcept { return {env, s, env->GetStringUTFChars(s, nullptr)}; }