Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=f29ac2f83aa2ff82f839eed56ee2ea172576a614

commit f29ac2f83aa2ff82f839eed56ee2ea172576a614
Author: Michel Hermier <herm...@frugalware.org>
Date:   Fri Nov 14 11:35:11 2014 +0100

libpacman: Add flib::str some compare/contains methods.

diff --git a/lib/libpacman/kernel/fstr.cpp b/lib/libpacman/kernel/fstr.cpp
index d91f6f8..c30fed6 100644
--- a/lib/libpacman/kernel/fstr.cpp
+++ b/lib/libpacman/kernel/fstr.cpp
@@ -105,9 +105,34 @@ str::size_type str::size() const
return 0;
}

-int str::compare(const str &s) const
+int str::compare(const char *str, case_sensitivity cs) const
{
-       return strcmp(c_str(), s.c_str());
+       switch(cs) {
+       case case_sensitive:
+               return strcmp(c_str(), str);
+       case case_insensitive:
+               return strcasecmp(c_str(), str);
+       default:
+               abort();
+       }
+}
+
+int str::indexOf(const char *str, size_type from, case_sensitivity cs) const
+{
+       ASSERT(from >= 0 && from <= size(), RET_ERR(PM_ERR_WRONG_ARGS, -1));
+       ASSERT(!empty(), RET_ERR(PM_ERR_WRONG_ARGS, -1));
+
+       const char *start = &m_str[from];
+       const char *index = NULL;
+       switch(cs) {
+       case case_sensitive:
+               index = strstr(c_str(), start);
+       case case_insensitive:
+               index = strcasestr(c_str(), start);
+       default:
+               abort();
+       }
+       return index != NULL ? index - str: -1;
}

void str::reset(const char *s)
diff --git a/lib/libpacman/kernel/fstr.h b/lib/libpacman/kernel/fstr.h
index 3c24258..913a99f 100644
--- a/lib/libpacman/kernel/fstr.h
+++ b/lib/libpacman/kernel/fstr.h
@@ -29,6 +29,12 @@
#include <regex.h>

namespace flib {
+       typedef enum
+       {
+               case_insensitive = 0,
+               case_sensitive = 1,
+       } case_sensitivity;
+
class str
{
public:
@@ -82,7 +88,40 @@ namespace flib {
size_type size() const;

/* Operations */
-               int compare(const str &str) const;
+               int compare(const char *str, case_sensitivity cs = 
case_sensitive) const;
+
+               int compare(const str &str, case_sensitivity cs = 
case_sensitive) const
+               {
+                       return compare(str.c_str(), cs);
+               }
+
+               int contains(const char *str, case_sensitivity cs = 
case_sensitive) const
+               {
+                       return indexOf(str, 0, cs);
+               }
+
+               int contains(const str &str, case_sensitivity cs = 
case_sensitive) const
+               {
+                       return indexOf(str, 0, cs);
+               }
+
+               bool equals(const char *str, case_sensitivity cs = 
case_sensitive) const
+               {
+                       return compare(str, cs) == 0;
+               }
+
+               bool equals(const str &str, case_sensitivity cs = 
case_sensitive) const
+               {
+                       return compare(str, cs) == 0;
+               }
+
+               int indexOf(const char *str, size_type from = 0, 
case_sensitivity cs = case_sensitive) const;
+
+               int indexOf(const str &str, size_type from = 0, 
case_sensitivity cs = case_sensitive) const
+               {
+                       return indexOf(str.c_str(), from, cs);
+               }
+
void reset(const char *s = nullptr);
void swap(str &o);
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to