Commit: 73454388fb4d026114928fecb131a52adcf63442
Author: Ankit Meel
Date:   Sat Aug 22 01:21:10 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB73454388fb4d026114928fecb131a52adcf63442

Use blender::StringRef instead of std::string_view.

Context:
{rB9616e2ef78f0}
{rBc521b69ffb68}

In MTL parser, `Vector.first_index_of_try` result was being compared
with `string::npos` which gave correct results since `npos` is -1,
but is wrong semantically. So fix it.

===================================================================

M       source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
M       source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
M       source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
M       source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh

===================================================================

diff --git 
a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc 
b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
index ae26e32bbe3..f16c0f81552 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
@@ -34,49 +34,48 @@
 namespace blender::io::obj {
 
 using std::string;
-using std::string_view;
 
 /**
  * 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(string_view line,
-                                string_view &r_line_key,
-                                string_view &r_rest_line)
+static void split_line_key_rest(StringRef line, StringRef &r_line_key, 
StringRef &r_rest_line)
 {
-  if (line.empty()) {
+  if (line.is_empty()) {
     return;
   }
 
-  const string_view::size_type pos{line.find_first_of(' ')};
-  if (pos == string_view::npos) {
+  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);
+    r_line_key = line.substr(0, pos_split);
   }
-  r_rest_line = line.substr(pos + 1, string_view::npos);
-  if (r_rest_line.empty()) {
+
+  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 string_view::size_type leading_space{r_rest_line.find_first_not_of(' 
')};
-  if (leading_space != string_view::npos) {
-    r_rest_line = r_rest_line.substr(leading_space, string_view::npos);
+  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 
new-line
-   * character and use it in the main getline. */
-  const string_view::size_type 
carriage_return{r_rest_line.find_first_of('\r')};
-  if (carriage_return != string_view::npos) {
-    r_rest_line = r_rest_line.substr(0, carriage_return);
+  /* 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 string_view::size_type trailing_space{r_rest_line.find_last_not_of(' 
')};
-  if (trailing_space != string_view::npos) {
+  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);
   }
@@ -86,29 +85,26 @@ static void split_line_key_rest(string_view line,
  * 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.
- * Caller should ensure that the given string has no leading spaces.
  */
-static void split_by_char(string_view in_string,
-                          const char delimiter,
-                          Vector<string_view> &r_out_list)
+static void split_by_char(StringRef in_string, const char delimiter, 
Vector<StringRef> &r_out_list)
 {
   r_out_list.clear();
-  while (!in_string.empty()) {
-    const string_view::size_type pos_delim{in_string.find_first_of(delimiter)};
-    const string_view::size_type word_len = pos_delim == string_view::npos ? 
in_string.size() :
-                                                                             
pos_delim;
 
-    string_view word{in_string.data(), word_len};
-    if (!word.empty() && !(word == " " && !(word[0] == '\0'))) {
+  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 == string_view::npos) {
+    if (pos_delim == StringRef::not_found) {
       return;
     }
     /* Skip the word already stored. */
-    in_string.remove_prefix(word_len);
+    in_string = in_string.drop_prefix(word_len);
     /* Skip all delimiters. */
-    in_string.remove_prefix(std::min(in_string.find_first_not_of(delimiter), 
in_string.size()));
+    in_string = 
in_string.drop_prefix(MIN2(in_string.find_first_not_of(delimiter), 
in_string.size()));
   }
 }
 
@@ -119,7 +115,7 @@ static void split_by_char(string_view in_string,
  * is set to the given fallback value in that case.
  */
 
-void copy_string_to_float(string_view src, const float fallback_value, float 
&r_dst)
+void copy_string_to_float(StringRef src, const float fallback_value, float 
&r_dst)
 {
   try {
     r_dst = std::stof(string(src));
@@ -142,7 +138,7 @@ void copy_string_to_float(string_view src, const float 
fallback_value, float &r_
  * Catches exception if any string cannot be converted to a float. The 
destination
  * float is set to the given fallback value in that case.
  */
-BLI_INLINE void copy_string_to_float(Span<string_view> src,
+BLI_INLINE void copy_string_to_float(Span<StringRef> src,
                                      const float fallback_value,
                                      MutableSpan<float> r_dst)
 {
@@ -158,7 +154,7 @@ BLI_INLINE void copy_string_to_float(Span<string_view> src,
  * Catches exception if the string cannot be converted to an integer. The 
destination
  * int is set to the given fallback value in that case.
  */
-BLI_INLINE void copy_string_to_int(string_view src, const int fallback_value, 
int &r_dst)
+BLI_INLINE void copy_string_to_int(StringRef src, const int fallback_value, 
int &r_dst)
 {
   try {
     r_dst = std::stoi(string(src));
@@ -180,7 +176,7 @@ BLI_INLINE void copy_string_to_int(string_view src, const 
int fallback_value, in
  * Catches exception if any string cannot be converted to an integer. The 
destination
  * int is set to the given fallback value in that case.
  */
-BLI_INLINE void copy_string_to_int(Span<string_view> src,
+BLI_INLINE void copy_string_to_int(Span<StringRef> src,
                                    const int fallback_value,
                                    MutableSpan<int> r_dst)
 {
@@ -198,13 +194,13 @@ BLI_INLINE void copy_string_to_int(Span<string_view> src,
  */
 static Geometry *create_geometry(Geometry *const prev_geometry,
                                  const eGeometryType new_type,
-                                 string_view name,
+                                 StringRef name,
                                  const GlobalVertices &global_vertices,
                                  Vector<std::unique_ptr<Geometry>> 
&r_all_geometries,
                                  VertexIndexOffset &r_offsets)
 {
   auto new_geometry = [&]() {
-    if (name.empty()) {
+    if (name.is_empty()) {
       r_all_geometries.append(std::make_unique<Geometry>(new_type, "New 
object"));
     }
     else {
@@ -278,9 +274,9 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
   string object_group{};
 
   while (std::getline(obj_file_, line)) {
-    string_view line_key, rest_line;
+    StringRef line_key, rest_line;
     split_line_key_rest(line, line_key, rest_line);
-    if (line.empty() || rest_line.empty()) {
+    if (line.empty() || rest_line.is_empty()) {
       continue;
     }
 
@@ -296,7 +292,7 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
     else if (line_key == "v") {
       BLI_assert(current_geometry);
       float3 curr_vert{};
-      Vector<string_view> str_vert_split;
+      Vector<StringRef> str_vert_split;
       split_by_char(rest_line, ' ', str_vert_split);
       copy_string_to_float(str_vert_split, FLT_MAX, {curr_vert, 3});
       global_vertices.vertices.append(curr_vert);
@@ -304,7 +300,7 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
     }
     else if (line_key == "vn") {
       float3 curr_vert_normal{};
-      Vector<string_view> str_vert_normal_split;
+      Vector<StringRef> str_vert_normal_split;
       split_by_char(rest_line, ' ', str_vert_normal_split);
       copy_string_to_float(str_vert_normal_split, FLT_MAX, {curr_vert_normal, 
2});
       global_vertices.vertex_normals.append(curr_vert_normal);
@@ -312,7 +308,7 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
     }
     else if (line_key == "vt") {
       float2 curr_uv_vert{};
-      Vector<string_view> str_uv_vert_split;
+      Vector<StringRef> str_uv_vert_split;
       split_by_char(rest_line, ' ', str_uv_vert_split);
       copy_string_to_float(str_uv_vert_split, FLT_MAX, {curr_uv_vert, 2});
       global_vertices.uv_vertices.append(curr_uv_vert);
@@ -320,7 +316,7 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
     else if (line_key == "l") {
       BLI_assert(current_geometry);
       int edge_v1 = -1, edge_v2 = -1;
-      Vector<string_view> str_edge_split;
+      Vector<StringRef> str_edge_split;
       split_by_char(rest_line, ' ', str_edge_split);
       copy_string_to_int(str_edge_split[0], -1, edge_v1);
       copy_string_to_int(str_edge_split[1], -1, edge_v2);
@@ -340,8 +336,8 @@ void 
OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
     }
     else if (line_key == "s") {
       /* Some implementations use "0" and "null" too, in addition to "off". */
-      if (rest_line != "0" && rest_line.find("off") == string::npos &&
-          rest_line.find("null") == string::npos) {
+      if (rest_line != "0" && rest_line.find("off") == StringRef::not_fou

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to