[hyena] [Hyena] Add StringUtil.EscapeLike



commit 0ab0ccace967c43c320f1a921c6a51078d31a7c5
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue May 25 19:44:45 2010 -0700

    [Hyena] Add StringUtil.EscapeLike

 src/Hyena/Hyena.Query/StringQueryValue.cs |    4 +---
 src/Hyena/Hyena/StringUtil.cs             |    9 +++++++++
 2 files changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/src/Hyena/Hyena.Query/StringQueryValue.cs b/src/Hyena/Hyena.Query/StringQueryValue.cs
index a707283..2572e91 100644
--- a/src/Hyena/Hyena.Query/StringQueryValue.cs
+++ b/src/Hyena/Hyena.Query/StringQueryValue.cs
@@ -90,9 +90,7 @@ namespace Hyena.Query
 
             if (op == Contains   || op == DoesNotContain ||
                 op == StartsWith || op == EndsWith) {
-                return orig.Replace ("\\", "\\\\")
-                           .Replace ("%", "\\%")
-                           .Replace ("_", "\\_");
+                return StringUtil.EscapeLike (orig);
             }
 
             return orig;
diff --git a/src/Hyena/Hyena/StringUtil.cs b/src/Hyena/Hyena/StringUtil.cs
index 8de7d3b..232641e 100644
--- a/src/Hyena/Hyena/StringUtil.cs
+++ b/src/Hyena/Hyena/StringUtil.cs
@@ -328,5 +328,14 @@ namespace Hyena
                 position = index + 1;
             }
         }
+
+        private static readonly char[] escaped_like_chars = new char[] {'\\', '%', '_'};
+        public static string EscapeLike (string s)
+        {
+            if (s.IndexOfAny (escaped_like_chars) != -1) {
+                return s.Replace (@"\", @"\\").Replace ("%", @"\%").Replace ("_", @"\_");
+            }
+            return s;
+        }
     }
 }



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