[glibmm: 1/2] Glib::Regex: Use UStringView



commit c84f13cf14f2436524d143c44601b8a59ac07a11
Author: Thomas Holder <thomas holder schrodinger com>
Date:   Thu Nov 21 11:12:14 2019 +0100

    Glib::Regex: Use UStringView
    
    Fixes #66

 glib/src/regex.ccg         | 34 ++++++++++++++---------------
 glib/src/regex.hg          | 54 +++++++++++++++++++++++-----------------------
 tests/Makefile.am          |  2 ++
 tests/glibmm_regex/main.cc | 28 ++++++++++++++++++++++++
 tools/m4/convert_glib.m4   |  2 ++
 5 files changed, 76 insertions(+), 44 deletions(-)
---
diff --git a/glib/src/regex.ccg b/glib/src/regex.ccg
index 86272bcd..e8095463 100644
--- a/glib/src/regex.ccg
+++ b/glib/src/regex.ccg
@@ -24,7 +24,7 @@ namespace Glib
 
 Glib::RefPtr<Glib::Regex>
 Regex::create(
-  const Glib::ustring& pattern, CompileFlags compile_options, MatchFlags match_options)
+  Glib::UStringView pattern, CompileFlags compile_options, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   auto regex = g_regex_new(
@@ -46,7 +46,7 @@ Regex::escape_string(const Glib::ustring& string)
 
 bool
 Regex::match(
-  const Glib::ustring& string, Glib::MatchInfo& match_info, MatchFlags match_options)
+  Glib::UStringView string, Glib::MatchInfo& match_info, MatchFlags match_options)
 {
   GMatchInfo* ginfo = nullptr;
   bool const result = static_cast<bool>(
@@ -56,13 +56,13 @@ Regex::match(
 }
 
 bool
-Regex::match(const Glib::ustring& string, MatchFlags match_options)
+Regex::match(Glib::UStringView string, MatchFlags match_options)
 {
   return g_regex_match(gobj(), string.c_str(), (GRegexMatchFlags)(match_options), nullptr);
 }
 
 bool
-Regex::match(const Glib::ustring& string, int start_position, Glib::MatchInfo& match_info,
+Regex::match(Glib::UStringView string, int start_position, Glib::MatchInfo& match_info,
   MatchFlags match_options)
 {
   GError* gerror = nullptr;
@@ -79,7 +79,7 @@ Regex::match(const Glib::ustring& string, int start_position, Glib::MatchInfo& m
 }
 
 bool
-Regex::match(const Glib::ustring& string, gssize string_len, int start_position,
+Regex::match(Glib::UStringView string, gssize string_len, int start_position,
   Glib::MatchInfo& match_info, MatchFlags match_options)
 {
   GError* gerror = nullptr;
@@ -96,7 +96,7 @@ Regex::match(const Glib::ustring& string, gssize string_len, int start_position,
 }
 
 bool
-Regex::match(const Glib::ustring& string, int start_position, MatchFlags match_options)
+Regex::match(Glib::UStringView string, int start_position, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   bool retvalue = g_regex_match_full(gobj(), string.c_str(), -1, start_position,
@@ -109,7 +109,7 @@ Regex::match(const Glib::ustring& string, int start_position, MatchFlags match_o
 
 bool
 Regex::match(
-  const Glib::ustring& string, gssize string_len, int start_position, MatchFlags match_options)
+  Glib::UStringView string, gssize string_len, int start_position, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   bool retvalue = g_regex_match_full(gobj(), string.c_str(), string_len, start_position,
@@ -122,7 +122,7 @@ Regex::match(
 
 bool
 Regex::match_all(
-  const Glib::ustring& string, Glib::MatchInfo& match_info, MatchFlags match_options)
+  Glib::UStringView string, Glib::MatchInfo& match_info, MatchFlags match_options)
 {
   GMatchInfo* ginfo = nullptr;
   bool const result = static_cast<bool>(g_regex_match_all(
@@ -132,13 +132,13 @@ Regex::match_all(
 }
 
 bool
-Regex::match_all(const Glib::ustring& string, MatchFlags match_options)
+Regex::match_all(Glib::UStringView string, MatchFlags match_options)
 {
   return g_regex_match_all(gobj(), string.c_str(), ((GRegexMatchFlags)(match_options)), nullptr);
 }
 
 bool
-Regex::match_all(const Glib::ustring& string, int start_position, Glib::MatchInfo& match_info,
+Regex::match_all(Glib::UStringView string, int start_position, Glib::MatchInfo& match_info,
   MatchFlags match_options)
 {
   GError* gerror = nullptr;
@@ -155,7 +155,7 @@ Regex::match_all(const Glib::ustring& string, int start_position, Glib::MatchInf
 }
 
 bool
-Regex::match_all(const Glib::ustring& string, gssize string_len, int start_position,
+Regex::match_all(Glib::UStringView string, gssize string_len, int start_position,
   Glib::MatchInfo& match_info, MatchFlags match_options)
 {
   GError* gerror = nullptr;
@@ -172,7 +172,7 @@ Regex::match_all(const Glib::ustring& string, gssize string_len, int start_posit
 }
 
 bool
-Regex::match_all(const Glib::ustring& string, int start_position, MatchFlags match_options)
+Regex::match_all(Glib::UStringView string, int start_position, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   bool retvalue = g_regex_match_all_full(gobj(), string.c_str(), -1, start_position,
@@ -185,7 +185,7 @@ Regex::match_all(const Glib::ustring& string, int start_position, MatchFlags mat
 
 bool
 Regex::match_all(
-  const Glib::ustring& string, gssize string_len, int start_position, MatchFlags match_options)
+  Glib::UStringView string, gssize string_len, int start_position, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   bool retvalue = g_regex_match_all_full(gobj(), string.c_str(), string_len, start_position,
@@ -197,7 +197,7 @@ Regex::match_all(
 }
 
 Glib::ustring
-Regex::replace(const Glib::ustring& string, int start_position, const Glib::ustring& replacement,
+Regex::replace(Glib::UStringView string, int start_position, Glib::UStringView replacement,
   MatchFlags match_options)
 {
   GError* gerror = nullptr;
@@ -210,8 +210,8 @@ Regex::replace(const Glib::ustring& string, int start_position, const Glib::ustr
 }
 
 Glib::ustring
-Regex::replace_literal(const Glib::ustring& string, int start_position,
-  const Glib::ustring& replacement, MatchFlags match_options)
+Regex::replace_literal(Glib::UStringView string, int start_position,
+  Glib::UStringView replacement, MatchFlags match_options)
 {
   GError* gerror = nullptr;
   auto retvalue =
@@ -224,7 +224,7 @@ Regex::replace_literal(const Glib::ustring& string, int start_position,
 }
 
 std::vector<Glib::ustring>
-Regex::split(const Glib::ustring& string, int start_position, MatchFlags match_options,
+Regex::split(Glib::UStringView string, int start_position, MatchFlags match_options,
   int max_tokens) const
 {
   GError* gerror = nullptr;
diff --git a/glib/src/regex.hg b/glib/src/regex.hg
index 68bca082..2d7c6037 100644
--- a/glib/src/regex.hg
+++ b/glib/src/regex.hg
@@ -100,36 +100,36 @@ public:
   _WRAP_ENUM(MatchFlags, GRegexMatchFlags, NO_GTYPE)
 
   /// @throws Glib::RegexError
-  static Glib::RefPtr<Glib::Regex> create(const Glib::ustring& pattern, CompileFlags compile_options = 
static_cast<CompileFlags>(0), MatchFlags match_options = static_cast<MatchFlags>(0));
+  static Glib::RefPtr<Glib::Regex> create(Glib::UStringView pattern, CompileFlags compile_options = 
static_cast<CompileFlags>(0), MatchFlags match_options = static_cast<MatchFlags>(0));
 
   _WRAP_METHOD(Glib::ustring get_pattern() const, g_regex_get_pattern)
   _WRAP_METHOD(int get_max_backref() const, g_regex_get_max_backref)
   _WRAP_METHOD(int get_capture_count() const, g_regex_get_capture_count)
   _WRAP_METHOD(bool get_has_cr_or_lf() const, g_regex_get_has_cr_or_lf)
   _WRAP_METHOD(int get_max_lookbehind() const, g_regex_get_max_lookbehind)
-  _WRAP_METHOD(int get_string_number(const Glib::ustring& name) const, g_regex_get_string_number)
+  _WRAP_METHOD(int get_string_number(Glib::UStringView name) const, g_regex_get_string_number)
   _WRAP_METHOD(CompileFlags get_compile_flags() const, g_regex_get_compile_flags)
   _WRAP_METHOD(MatchFlags get_match_flags() const, g_regex_get_match_flags)
 
   static Glib::ustring escape_string(const Glib::ustring& string);
 
-  _WRAP_METHOD(static bool match_simple(const Glib::ustring& pattern, const Glib::ustring& string, 
CompileFlags compile_options = static_cast<CompileFlags>(0), MatchFlags match_options = 
static_cast<MatchFlags>(0)), g_regex_match_simple)
+  _WRAP_METHOD(static bool match_simple(Glib::UStringView pattern, Glib::UStringView string, CompileFlags 
compile_options = static_cast<CompileFlags>(0), MatchFlags match_options = static_cast<MatchFlags>(0)), 
g_regex_match_simple)
 
   _WRAP_METHOD_DOCS_ONLY(g_regex_match)
   bool match(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     Glib::MatchInfo& match_info,
     MatchFlags match_options = static_cast<MatchFlags>(0)
   );
 
   /// A match() method not requiring a Glib::MatchInfo.
-  bool match(const Glib::ustring& string, MatchFlags match_options = static_cast<MatchFlags>(0));
+  bool match(Glib::UStringView string, MatchFlags match_options = static_cast<MatchFlags>(0));
 
   /** A match() method with a start position and a Glib::MatchInfo.
    * @throws Glib::RegexError
    */
   bool match(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     int start_position,
     Glib::MatchInfo& match_info,
     MatchFlags match_options = static_cast<MatchFlags>(0)
@@ -137,7 +137,7 @@ public:
 
   _WRAP_METHOD_DOCS_ONLY(g_regex_match_full, errthrow "Glib::RegexError")
   bool match(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     gssize string_len,
     int start_position,
     Glib::MatchInfo& match_info,
@@ -147,28 +147,28 @@ public:
   /** A match() method with a start position not requiring a Glib::MatchInfo.
    * @throws Glib::RegexError
    */
-  bool match(const Glib::ustring& string, int start_position, MatchFlags match_options);
+  bool match(Glib::UStringView string, int start_position, MatchFlags match_options);
 
   /** A match() method with a string length and start position not requiring a
    * Glib::MatchInfo.
    */
-  bool match(const Glib::ustring& string, gssize string_len, int start_position, MatchFlags match_options);
+  bool match(Glib::UStringView string, gssize string_len, int start_position, MatchFlags match_options);
 
   _WRAP_METHOD_DOCS_ONLY(g_regex_match_all)
   bool match_all(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     Glib::MatchInfo& match_info,
     MatchFlags match_options = static_cast<MatchFlags>(0)
   );
 
   /// A match_all() method not requiring a Glib::MatchInfo.
-  bool match_all(const Glib::ustring& string, MatchFlags match_options = static_cast<MatchFlags>(0));
+  bool match_all(Glib::UStringView string, MatchFlags match_options = static_cast<MatchFlags>(0));
 
   /** A match_all() method with a start positon and a Glib::MatchInfo.
    * @throws Glib::RegexError
    */
   bool match_all(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     int start_position,
     Glib::MatchInfo& match_info,
     MatchFlags match_options = static_cast<MatchFlags>(0)
@@ -176,7 +176,7 @@ public:
 
   _WRAP_METHOD_DOCS_ONLY(g_regex_match_all_full, errthrow "Glib::RegexError")
   bool match_all(
-    const Glib::ustring& string,
+    Glib::UStringView string,
     gssize string_len,
     int start_position,
     Glib::MatchInfo& match_info,
@@ -186,33 +186,33 @@ public:
   /** A match_all() method with a start position not requiring a Glib::MatchInfo.
    * @throws Glib::RegexError
    */
-  bool match_all(const Glib::ustring& string, int start_position, MatchFlags match_options);
+  bool match_all(Glib::UStringView string, int start_position, MatchFlags match_options);
 
   /** A match_all() method with a start position and a string length not
    * requiring a Glib::MatchInfo.
    * @throws Glib::RegexError
    */
-  bool match_all(const Glib::ustring& string, gssize string_len, int start_position, MatchFlags 
match_options);
+  bool match_all(Glib::UStringView string, gssize string_len, int start_position, MatchFlags match_options);
 
 #m4 
_CONVERSION(`gchar**',`std::vector<Glib::ustring>',`Glib::ArrayHandler<Glib::ustring>::array_to_vector($3, 
Glib::OWNERSHIP_DEEP)')
-  _WRAP_METHOD(static std::vector<Glib::ustring> split_simple(const Glib::ustring& pattern, const 
Glib::ustring& string, CompileFlags compile_options = static_cast<CompileFlags>(0), MatchFlags match_options 
= static_cast<MatchFlags>(0)), g_regex_split_simple)
-  _WRAP_METHOD(std::vector<Glib::ustring> split(const Glib::ustring& string, MatchFlags match_options = 
static_cast<MatchFlags>(0)), g_regex_split)
+  _WRAP_METHOD(static std::vector<Glib::ustring> split_simple(Glib::UStringView pattern, Glib::UStringView 
string, CompileFlags compile_options = static_cast<CompileFlags>(0), MatchFlags match_options = 
static_cast<MatchFlags>(0)), g_regex_split_simple)
+  _WRAP_METHOD(std::vector<Glib::ustring> split(Glib::UStringView string, MatchFlags match_options = 
static_cast<MatchFlags>(0)), g_regex_split)
 
   _WRAP_METHOD(std::vector<Glib::ustring> split(const gchar* string, gssize string_len, int start_position, 
MatchFlags match_options = static_cast<MatchFlags>(0), int max_tokens = 0) const, g_regex_split_full, 
errthrow "Glib::RegexError")
 
   /// @throws Glib::RegexError
-  std::vector<Glib::ustring> split(const Glib::ustring& string, int start_position, MatchFlags 
match_options, int max_tokens) const;
+  std::vector<Glib::ustring> split(Glib::UStringView string, int start_position, MatchFlags match_options, 
int max_tokens) const;
 
-  _WRAP_METHOD(Glib::ustring replace(const gchar* string, gssize string_len, int start_position, const 
Glib::ustring& replacement, MatchFlags match_options = static_cast<MatchFlags>(0)), g_regex_replace, errthrow 
"Glib::RegexError")
+  _WRAP_METHOD(Glib::ustring replace(const gchar* string, gssize string_len, int start_position, 
Glib::UStringView replacement, MatchFlags match_options = static_cast<MatchFlags>(0)), g_regex_replace, 
errthrow "Glib::RegexError")
   /// @throws Glib::RegexError
-  Glib::ustring replace(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, 
MatchFlags match_options);
+  Glib::ustring replace(Glib::UStringView string, int start_position, Glib::UStringView replacement, 
MatchFlags match_options);
 
-  _WRAP_METHOD(Glib::ustring replace_literal(const gchar *string, gssize string_len, int start_position, 
const Glib::ustring& replacement, MatchFlags match_options = static_cast<MatchFlags>(0)), 
g_regex_replace_literal, errthrow "Glib::RegexError")
+  _WRAP_METHOD(Glib::ustring replace_literal(const gchar *string, gssize string_len, int start_position, 
Glib::UStringView replacement, MatchFlags match_options = static_cast<MatchFlags>(0)), 
g_regex_replace_literal, errthrow "Glib::RegexError")
   /// @throws Glib::RegexError
-  Glib::ustring replace_literal(const Glib::ustring& string, int start_position, const Glib::ustring& 
replacement, MatchFlags match_options);
+  Glib::ustring replace_literal(Glib::UStringView string, int start_position, Glib::UStringView replacement, 
MatchFlags match_options);
 
-  _WRAP_METHOD(Glib::ustring replace_eval(const Glib::ustring& string, gssize string_len, int 
start_position, MatchFlags match_options, GRegexEvalCallback eval,  gpointer user_data), 
g_regex_replace_eval, errthrow "Glib::RegexError")
-  _WRAP_METHOD(static bool check_replacement(const Glib::ustring& replacement, gboolean* has_references), 
g_regex_check_replacement, errthrow "Glib::RegexError")
+  _WRAP_METHOD(Glib::ustring replace_eval(Glib::UStringView string, gssize string_len, int start_position, 
MatchFlags match_options, GRegexEvalCallback eval,  gpointer user_data), g_regex_replace_eval, errthrow 
"Glib::RegexError")
+  _WRAP_METHOD(static bool check_replacement(Glib::UStringView replacement, gboolean* has_references), 
g_regex_check_replacement, errthrow "Glib::RegexError")
 };
 
 //TODO: Add C++ iterator like functionality for this class.
@@ -269,15 +269,15 @@ public:
   _WRAP_METHOD(int get_match_count() const, g_match_info_get_match_count)
   _WRAP_METHOD(bool is_partial_match() const, g_match_info_is_partial_match)
 
-  _WRAP_METHOD(Glib::ustring expand_references(const Glib::ustring& string_to_expand), 
g_match_info_expand_references, errthrow "Glib::RegexError")
+  _WRAP_METHOD(Glib::ustring expand_references(Glib::UStringView string_to_expand), 
g_match_info_expand_references, errthrow "Glib::RegexError")
 
   _WRAP_METHOD(Glib::ustring fetch(int match_num), g_match_info_fetch)
 
   _WRAP_METHOD(bool fetch_pos(int match_num, int& start_pos, int& end_pos), g_match_info_fetch_pos)
 
-  _WRAP_METHOD(Glib::ustring fetch_named(const Glib::ustring& name), g_match_info_fetch_named)
+  _WRAP_METHOD(Glib::ustring fetch_named(Glib::UStringView name), g_match_info_fetch_named)
 
-  _WRAP_METHOD(bool fetch_named_pos(const Glib::ustring& name, int& start_pos, int& end_pos), 
g_match_info_fetch_named_pos)
+  _WRAP_METHOD(bool fetch_named_pos(Glib::UStringView name, int& start_pos, int& end_pos), 
g_match_info_fetch_named_pos)
 
   _WRAP_METHOD(std::vector<Glib::ustring> fetch_all(), g_match_info_fetch_all)
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3905c52f..68effc10 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -39,6 +39,7 @@ check_PROGRAMS =                              \
        glibmm_object_move/test                 \
        glibmm_objectbase/test                  \
        glibmm_objectbase_move/test                     \
+       glibmm_regex/test                       \
        glibmm_ustring_compose/test             \
        glibmm_ustring_format/test              \
        glibmm_ustring_sprintf/test             \
@@ -117,6 +118,7 @@ glibmm_objectbase_move_test_SOURCES      = glibmm_objectbase_move/main.cc \
 glibmm_ustring_compose_test_SOURCES      = glibmm_ustring_compose/main.cc
 glibmm_ustring_format_test_SOURCES       = glibmm_ustring_format/main.cc
 glibmm_ustring_sprintf_test_SOURCES      = glibmm_ustring_sprintf/main.cc
+glibmm_regex_test_SOURCES                = glibmm_regex/main.cc
 glibmm_value_test_SOURCES                = glibmm_value/main.cc
 glibmm_variant_test_SOURCES              = glibmm_variant/main.cc
 glibmm_vector_test_SOURCES               = glibmm_vector/main.cc
diff --git a/tests/glibmm_regex/main.cc b/tests/glibmm_regex/main.cc
new file mode 100644
index 00000000..1b049d28
--- /dev/null
+++ b/tests/glibmm_regex/main.cc
@@ -0,0 +1,28 @@
+#include <glibmm/regex.h>
+
+static void
+test_match_string_literal()
+{
+  auto regex = Glib::Regex::create("(\\S+)");
+  Glib::MatchInfo matchInfo;
+
+  regex->match("this is not a Glib::ustring const reference", matchInfo);
+
+  for (const char* s : { "this", "is", "not", "a", "Glib::ustring", "const", "reference" })
+  {
+    g_assert_true(matchInfo.matches());
+    g_assert_true(matchInfo.fetch(1) == s);
+    matchInfo.next();
+  }
+
+  g_assert_false(matchInfo.matches());
+}
+
+int
+main()
+{
+  // https://gitlab.gnome.org/GNOME/glibmm/issues/66
+  test_match_string_literal();
+
+  return EXIT_SUCCESS;
+}
diff --git a/tools/m4/convert_glib.m4 b/tools/m4/convert_glib.m4
index c7df34e9..e9dd22b2 100644
--- a/tools/m4/convert_glib.m4
+++ b/tools/m4/convert_glib.m4
@@ -99,6 +99,8 @@ _CONVERSION(`const char*',`const-gchar*',`$3')
 _CONVERSION(`const-gchar*',`const char*',`$3')
 _CONVERSION(`const char*',`const std::string&',__GCHARP_TO_STDSTRING)
 
+_CONVERSION(`Glib::UStringView',`const char*',`$3.c_str()')
+
 _CONVERSION(`return-gchar*',`Glib::ustring',`Glib::convert_return_gchar_ptr_to_ustring($3)')
 _CONVERSION(`return-gchar*',`std::string',`Glib::convert_return_gchar_ptr_to_stdstring($3)')
 _CONVERSION(`return-char*',`Glib::ustring',`Glib::convert_return_gchar_ptr_to_ustring($3)')


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]