Path: blob/master/modules/mono/utils/string_utils.cpp
10279 views
/**************************************************************************/1/* string_utils.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "string_utils.h"3132#include "core/io/file_access.h"3334#include <cstdio>35#include <cstdlib>3637namespace {3839int sfind(const String &p_text, int p_from) {40if (p_from < 0) {41return -1;42}4344int src_len = 2;45int len = p_text.length();4647if (len == 0) {48return -1;49}5051const char32_t *src = p_text.get_data();5253for (int i = p_from; i <= (len - src_len); i++) {54bool found = true;5556for (int j = 0; j < src_len; j++) {57int read_pos = i + j;5859ERR_FAIL_COND_V(read_pos >= len, -1);6061switch (j) {62case 0:63found = src[read_pos] == '%';64break;65case 1: {66char32_t c = src[read_pos];67found = src[read_pos] == 's' || (c >= '0' && c <= '5');68break;69}70default:71found = false;72}7374if (!found) {75break;76}77}7879if (found) {80return i;81}82}8384return -1;85}86} // namespace8788String sformat(const String &p_text, const String &p1, const String &p2,89const String &p3, const String &p4, const String &p5, const String &p6) {90if (p_text.length() < 2) {91return p_text;92}9394String args[6] = { p1, p2, p3, p4, p5, p6 };9596String new_string;9798int findex = 0;99int search_from = 0;100int result = 0;101102while ((result = sfind(p_text, search_from)) >= 0) {103char32_t c = p_text[result + 1];104105int req_index = (c == 's' ? findex++ : c - '0');106107new_string += p_text.substr(search_from, result - search_from);108new_string += args[req_index];109search_from = result + 2;110}111112new_string += p_text.substr(search_from);113114return new_string;115}116117#ifdef TOOLS_ENABLED118bool is_csharp_keyword(const String &p_name) {119// Reserved keywords120121return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||122p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||123p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||124p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||125p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||126p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||127p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||128p_name == "foreach" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||129p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||130p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||131p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||132p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||133p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||134p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||135p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||136p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||137p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||138p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||139p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";140}141142String escape_csharp_keyword(const String &p_name) {143return is_csharp_keyword(p_name) ? "@" + p_name : p_name;144}145#endif146147Error read_all_file_utf8(const String &p_path, String &r_content) {148Vector<uint8_t> sourcef;149Error err;150Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);151ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");152153uint64_t len = f->get_length();154sourcef.resize(len + 1);155uint8_t *w = sourcef.ptrw();156uint64_t r = f->get_buffer(w, len);157ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);158w[len] = 0;159160String source;161if (source.append_utf8((const char *)w, len) != OK) {162ERR_FAIL_V(ERR_INVALID_DATA);163}164165r_content = source;166return OK;167}168169// TODO: Move to variadic templates once we upgrade to C++11170171String str_format(const char *p_format, ...) {172va_list list;173174va_start(list, p_format);175String res = str_format(p_format, list);176va_end(list);177178return res;179}180181#if defined(MINGW_ENABLED)182#define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)183#define RScprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)184#else185#define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)186#define RScprintf(m_format, m_args_copy) vsnprintf(nullptr, 0, p_format, m_args_copy)187#endif188189String str_format(const char *p_format, va_list p_list) {190char *buffer = str_format_new(p_format, p_list);191192String res(buffer);193memdelete_arr(buffer);194195return res;196}197198char *str_format_new(const char *p_format, ...) {199va_list list;200201va_start(list, p_format);202char *res = str_format_new(p_format, list);203va_end(list);204205return res;206}207208char *str_format_new(const char *p_format, va_list p_list) {209va_list list;210211va_copy(list, p_list);212int len = RScprintf(p_format, list);213va_end(list);214215len += 1; // for the trailing '/0'216217char *buffer(memnew_arr(char, len));218219va_copy(list, p_list);220RSnprintf(buffer, len, p_format, list);221va_end(list);222223return buffer;224}225226227