[Bf-blender-cvs] [6f7d03b643b] soc-2020-io-performance: Move string utility functions in one file.
Ankit Meel
noreply at git.blender.org
Sat Sep 5 14:14:37 CEST 2020
Commit: 6f7d03b643be993665e441c57372b5c5cf093b08
Author: Ankit Meel
Date: Fri Sep 4 01:47:06 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB6f7d03b643be993665e441c57372b5c5cf093b08
Move string utility functions in one file.
No functional change. Only renaming, and adding a few const.
===================================================================
M source/blender/io/wavefront_obj/CMakeLists.txt
M source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
M source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
A source/blender/io/wavefront_obj/intern/string_utils.cc
A source/blender/io/wavefront_obj/intern/string_utils.hh
===================================================================
diff --git a/source/blender/io/wavefront_obj/CMakeLists.txt b/source/blender/io/wavefront_obj/CMakeLists.txt
index 568d14332dd..8ee17ce9114 100644
--- a/source/blender/io/wavefront_obj/CMakeLists.txt
+++ b/source/blender/io/wavefront_obj/CMakeLists.txt
@@ -50,6 +50,7 @@ set(SRC
intern/obj_import_mtl.cc
intern/obj_import_nurbs.cc
intern/obj_import_objects.cc
+ intern/string_utils.cc
intern/wavefront_obj.cc
IO_wavefront_obj.h
@@ -64,6 +65,7 @@ set(SRC
intern/obj_import_mesh.hh
intern/obj_import_mtl.hh
intern/obj_import_nurbs.hh
+ intern/string_utils.hh
intern/obj_import_objects.hh
)
diff --git a/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
index 457bc8c59dd..a4d73d94ec2 100644
--- a/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
@@ -30,181 +30,12 @@
#include "obj_export_file_writer.hh"
#include "obj_import_file_reader.hh"
+#include "string_utils.hh"
namespace blender::io::obj {
using std::string;
-/**
- * Store multiple lines separated by an escaped newline character: `\\n`.
- * Use this before doing any parse operations on the read string.
- */
-static void read_next_line(std::ifstream &file, string &r_line)
-{
- std::string new_line;
- while (file.good() && !r_line.empty() && r_line.back() == '\\') {
- new_line.clear();
- bool ok = static_cast<bool>(std::getline(file, new_line));
- /* Remove the last backslash character. */
- r_line.pop_back();
- r_line.append(new_line);
- if (!ok || new_line.empty()) {
- return;
- }
- }
-}
-
-/**
- * Split a line string into the first word (key) and the rest of the line.
- * Also remove leading & trailing spaces as well as `\r` carriage return
- * character if present.
- */
-static void split_line_key_rest(StringRef line, StringRef &r_line_key, StringRef &r_rest_line)
-{
- if (line.is_empty()) {
- return;
- }
-
- const int64_t pos_split{line.find_first_of(' ')};
- if (pos_split == StringRef::not_found) {
- /* Use the first character if no space is found in the line. It's usually a comment like:
- * #This is a comment. */
- r_line_key = line.substr(0, 1);
- }
- else {
- r_line_key = line.substr(0, pos_split);
- }
-
- r_rest_line = line = line.drop_prefix(r_line_key.size());
- if (r_rest_line.is_empty()) {
- return;
- }
-
- /* Remove any leading spaces, trailing spaces & \r character, if any. */
- const int64_t leading_space{r_rest_line.find_first_not_of(' ')};
- if (leading_space != StringRef::not_found) {
- r_rest_line = r_rest_line.drop_prefix(leading_space);
- }
-
- /* Another way is to do a test run before the actual parsing to find the newline
- * character and use it in the getline. */
- const int64_t carriage_return{r_rest_line.find_first_of('\r')};
- if (carriage_return != StringRef::not_found) {
- r_rest_line = r_rest_line.drop_prefix(carriage_return);
- }
-
- const int64_t trailing_space{r_rest_line.find_last_not_of(' ')};
- if (trailing_space != StringRef::not_found) {
- /* The position is of a character that is not ' ', so count of characters is position + 1. */
- r_rest_line = r_rest_line.substr(0, trailing_space + 1);
- }
-}
-
-/**
- * Split the given string by the delimiter and fill the given vector.
- * If an intermediate string is empty, or space or null character, it is not appended to the
- * vector.
- */
-static void split_by_char(StringRef in_string, const char delimiter, Vector<StringRef> &r_out_list)
-{
- r_out_list.clear();
-
- while (!in_string.is_empty()) {
- const int64_t pos_delim{in_string.find_first_of(delimiter)};
- const int64_t word_len = pos_delim == StringRef::not_found ? in_string.size() : pos_delim;
-
- StringRef word{in_string.data(), word_len};
- if (!word.is_empty() && !(word == " " && !(word[0] == '\0'))) {
- r_out_list.append(word);
- }
- if (pos_delim == StringRef::not_found) {
- return;
- }
- /* Skip the word already stored. */
- in_string = in_string.drop_prefix(word_len);
- /* Skip all delimiters. */
- in_string = in_string.drop_prefix(
- std::min(in_string.find_first_not_of(delimiter), in_string.size()));
- }
-}
-
-/**
- * Convert the given string to float and assign it to the destination value.
- *
- * Catches exception if the string cannot be converted to a float. The destination value
- * is set to the given fallback value in that case.
- */
-
-static void copy_string_to_float(StringRef src, const float fallback_value, float &r_dst)
-{
- try {
- r_dst = std::stof(string(src));
- }
- catch (const std::invalid_argument &inv_arg) {
- std::cerr << "Bad conversion to float:'" << inv_arg.what() << "':'" << src << "'" << std::endl;
- r_dst = fallback_value;
- }
- catch (const std::out_of_range &out_of_range) {
- std::cerr << "Out of range for float:'" << out_of_range.what() << ":'" << src << "'"
- << std::endl;
- r_dst = fallback_value;
- }
-}
-
-/**
- * Convert all members of the Span of strings to floats and assign them to the float
- * array members. Usually used for values like coordinates.
- *
- * Catches exception if any string cannot be converted to a float. The destination
- * float is set to the given fallback value in that case.
- */
-static void copy_string_to_float(Span<StringRef> src,
- const float fallback_value,
- MutableSpan<float> r_dst)
-{
- BLI_assert(src.size() >= r_dst.size());
- for (int i = 0; i < r_dst.size(); ++i) {
- copy_string_to_float(src[i], fallback_value, r_dst[i]);
- }
-}
-
-/**
- * Convert the given string to int and assign it to the destination value.
- *
- * Catches exception if the string cannot be converted to an integer. The destination
- * int is set to the given fallback value in that case.
- */
-static void copy_string_to_int(StringRef src, const int fallback_value, int &r_dst)
-{
- try {
- r_dst = std::stoi(string(src));
- }
- catch (const std::invalid_argument &inv_arg) {
- std::cerr << "Bad conversion to int:'" << inv_arg.what() << "':'" << src << "'" << std::endl;
- r_dst = fallback_value;
- }
- catch (const std::out_of_range &out_of_range) {
- std::cerr << "Out of range for int:'" << out_of_range.what() << ":'" << src << "'"
- << std::endl;
- r_dst = fallback_value;
- }
-}
-
-/**
- * Convert the given strings to ints and fill the destination int buffer.
- *
- * Catches exception if any string cannot be converted to an integer. The destination
- * int is set to the given fallback value in that case.
- */
-static void copy_string_to_int(Span<StringRef> src,
- const int fallback_value,
- MutableSpan<int> r_dst)
-{
- BLI_assert(src.size() >= r_dst.size());
- for (int i = 0; i < r_dst.size(); ++i) {
- copy_string_to_int(src[i], fallback_value, r_dst[i]);
- }
-}
/**
* Based on the properties of the given Geometry instance, create a new Geometry instance
diff --git a/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc b/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
index 27142e77a8f..90f5d743f02 100644
--- a/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
@@ -31,6 +31,7 @@
#include "NOD_shader.h"
#include "obj_import_mtl.hh"
+#include "string_utils.hh"
namespace blender::io::obj {
@@ -72,18 +73,6 @@ static void set_property_of_socket(eNodeSocketDatatype property_type,
}
}
-static std::string replace_all_occurences(StringRef path, StringRef to_remove, StringRef to_add)
-{
- std::string clean_path{path};
- while (true) {
- std::string::size_type pos = clean_path.find(to_remove);
- if (pos == std::string::npos) {
- break;
- }
- clean_path.replace(pos, to_add.size(), to_add);
- }
- return clean_path;
-}
/**
* Load image for Image Texture node and set the node properties.
diff --git a/source/blender/io/wavefront_obj/intern/string_utils.cc b/source/blender/io/wavefront_obj/intern/string_utils.cc
new file mode 100644
index 00000000000..ca540f575a3
--- /dev/null
+++ b/source/blender/io/wavefront_obj/intern/string_utils.cc
@@ -0,0 +1,215 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation.
+ * All rights reserved.
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include "BLI_float3.hh"
+#include "BLI_span.hh"
+#include "BLI_string_ref.hh"
+#include "BLI_vector.hh"
+
+#include "string_utils.hh"
+
+namespace blender::io::obj {
+using std::string;
+
+/**
+ * Store multiple lines separated by an escaped newline character: `\\n`.
+ * Use this before doing any parse operations on the read string.
+ */
+void read_next_line(std::ifstream &file, string &r_line)
+{
+ std::string new_line;
+ while (file.good() && !r_line.empty() && r_line.back() == '\\') {
+ new_line.clear();
+ const bool ok = static_cast<bool>(std::getline(file, new_line));
+ /* Remove the last backslash character. */
+ r_line.pop_back();
+ r_line.append(new_line);
+ if (!ok || new_line.empty()) {
+ return;
+ }
+ }
+}
+
+/**
+ * Split a line string into the first word (key) and the rest of the line.
+ * Also remove leading & trailing spaces as well as `\r` carriage return
+ * character if present.
+ */
+void split_line_key_rest(S
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list