summaryrefslogtreecommitdiff
path: root/modules/print.cpp
diff options
context:
space:
mode:
authorPhilip Chimento <philip.chimento@gmail.com>2020-11-24 19:27:55 -0800
committerPhilip Chimento <philip.chimento@gmail.com>2021-01-02 12:26:56 -0800
commitfee940c0710f6bc3f9b08a1cef26ec4555cbb440 (patch)
treea288f0f7d40d962c2243387cb0f20c9b4c43b12f /modules/print.cpp
parentc5b07deb99a329fe59967063c706697d06296f2e (diff)
downloadgjs-fee940c0710f6bc3f9b08a1cef26ec4555cbb440.tar.gz
js: Use strings and stringstreams instead of GString
std::ostringstream is the C++ standard library version of GString. If we are not doing any formatting but only appending other strings, then we can even just use std::string and the + operator. This allows using RAII so we can get rid of a goto, and just generally be more memory-safe. We continue using GString in gjs_hyphen_from_camel() for performance reasons, because we would like a modifiable C buffer that we can pass to canonicalize_key(), and the only way to get a modifiable buffer out of std::string would be to copy it. Note: stream.str() _creates_ a std::string object, and copies the string data. string.c_str() returns a const char* _without_ copying.
Diffstat (limited to 'modules/print.cpp')
-rw-r--r--modules/print.cpp29
1 files changed, 13 insertions, 16 deletions
diff --git a/modules/print.cpp b/modules/print.cpp
index e73a35af..cb0f1e3b 100644
--- a/modules/print.cpp
+++ b/modules/print.cpp
@@ -5,6 +5,8 @@
#include <config.h>
+#include <string>
+
#include <glib.h>
#include <js/CallArgs.h>
@@ -85,8 +87,9 @@ static bool gjs_log_error(JSContext* cx, unsigned argc, JS::Value* vp) {
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_print_parse_args(JSContext* cx, const JS::CallArgs& argv,
- GjsAutoChar* buffer) {
- GString* str = g_string_new("");
+ std::string* buffer) {
+ g_assert(buffer && "forgot out parameter");
+ buffer->clear();
for (unsigned n = 0; n < argv.length(); ++n) {
/* JS::ToString might throw, in which case we will only log that the
* value could not be converted to string */
@@ -96,23 +99,17 @@ static bool gjs_print_parse_args(JSContext* cx, const JS::CallArgs& argv,
if (jstr) {
JS::UniqueChars s(JS_EncodeStringToUTF8(cx, jstr));
- if (!s) {
- g_string_free(str, true);
+ if (!s)
return false;
- }
- g_string_append(str, s.get());
+ *buffer += s.get();
if (n < (argv.length() - 1))
- g_string_append_c(str, ' ');
+ *buffer += ' ';
} else {
- *buffer = g_string_free(str, true);
- if (!*buffer)
- *buffer = g_strdup("<invalid string>");
+ *buffer = "<invalid string>";
return true;
}
}
- *buffer = g_string_free(str, false);
-
return true;
}
@@ -120,11 +117,11 @@ GJS_JSAPI_RETURN_CONVENTION
static bool gjs_print(JSContext* context, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
- GjsAutoChar buffer;
+ std::string buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
- g_print("%s\n", buffer.get());
+ g_print("%s\n", buffer.c_str());
argv.rval().setUndefined();
return true;
@@ -134,11 +131,11 @@ GJS_JSAPI_RETURN_CONVENTION
static bool gjs_printerr(JSContext* context, unsigned argc, JS::Value* vp) {
JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
- GjsAutoChar buffer;
+ std::string buffer;
if (!gjs_print_parse_args(context, argv, &buffer))
return false;
- g_printerr("%s\n", buffer.get());
+ g_printerr("%s\n", buffer.c_str());
argv.rval().setUndefined();
return true;