summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2011-08-28 00:17:42 +0100
committerCaolán McNamara <caolanm@redhat.com>2011-08-29 09:56:06 +0100
commit14d2a60053e30dcb7e6956637fe8d57d18563e3f (patch)
treea395e8185afde2031fa255094ee9b0d23f11afe9 /comphelper
parent65302eb1bed16db8f06cbb048d03ba6d644b3fb6 (diff)
remove ByteString::IsAlphaNumericAscii and refactor a bit
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/inc/comphelper/string.hxx52
-rw-r--r--comphelper/qa/string/test_string.cxx31
-rw-r--r--comphelper/source/misc/string.cxx32
3 files changed, 103 insertions, 12 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx
index 3e74ca452aaa..4daa01b00b6f 100644
--- a/comphelper/inc/comphelper/string.hxx
+++ b/comphelper/inc/comphelper/string.hxx
@@ -268,25 +268,65 @@ public:
const ::com::sun::star::lang::Locale& getLocale() const { return m_aLocale; }
};
-/** Determine if an OString contains solely ascii numeric digits
+/** Determine if an OString contains solely ASCII numeric digits
@param rString An OString
@return false if string contains any characters outside
- the ascii '0'-'9' range
+ the ASCII '0'-'9' range
true otherwise, including for empty string
*/
-COMPHELPER_DLLPUBLIC bool isAsciiDecimalString(const rtl::OString &rString);
+COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const rtl::OString &rString);
-/** Determine if an OUString contains solely ascii numeric digits
+/** Determine if an OUString contains solely ASCII numeric digits
@param rString An OUString
@return false if string contains any characters outside
- the ascii '0'-'9' range
+ the ASCII '0'-'9' range
true otherwise, including for empty string
*/
-COMPHELPER_DLLPUBLIC bool isAsciiDecimalString(const rtl::OUString &rString);
+COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const rtl::OUString &rString);
+
+/** Determine if an OString contains solely ASCII alphanumeric digits
+
+ @param rString An OString
+
+ @return false if string contains any characters outside
+ the ASCII 'a'-'z', 'A'-'Z' and '0'-'9' ranges
+ true otherwise, including for empty string
+ */
+COMPHELPER_DLLPUBLIC bool isalnumAsciiString(const rtl::OString &rString);
+
+/** Determine if an OUString contains solely ASCII alphanumeric digits
+
+ @param rString An OUString
+
+ @return false if string contains any characters outside
+ the ASCII 'a'-'z', 'A'-'Z' and '0'-'9' ranges
+ true otherwise, including for empty string
+ */
+COMPHELPER_DLLPUBLIC bool isalnumAsciiString(const rtl::OString &rString);
+
+COMPHELPER_DLLPUBLIC inline bool isdigitAscii(sal_Unicode c)
+{
+ return ((c >= '0') && (c <= '9'));
+}
+
+COMPHELPER_DLLPUBLIC inline bool islowerAscii(sal_Unicode c)
+{
+ return ((c >= 'a') && (c <= 'z'));
+}
+
+COMPHELPER_DLLPUBLIC inline bool isupperAscii(sal_Unicode c)
+{
+ return ((c >= 'A') && (c <= 'Z'));
+}
+
+COMPHELPER_DLLPUBLIC inline bool isalnumAscii(sal_Unicode c)
+{
+ return isdigitAscii(c) || islowerAscii(c) || isupperAscii(c);
+}
} }
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx
index 4ac96dccc5f7..d7187e04fd75 100644
--- a/comphelper/qa/string/test_string.cxx
+++ b/comphelper/qa/string/test_string.cxx
@@ -46,6 +46,8 @@ public:
void testReplace();
void testToken();
void testDecimalStringToNumber();
+ void testIsdigitAsciiString();
+ void testIsalnumAsciiString();
CPPUNIT_TEST_SUITE(TestString);
CPPUNIT_TEST(testSearchAndReplaceAsciiL);
@@ -53,6 +55,8 @@ public:
CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testToken);
CPPUNIT_TEST(testDecimalStringToNumber);
+ CPPUNIT_TEST(testIsdigitAsciiString);
+ CPPUNIT_TEST(testIsalnumAsciiString);
CPPUNIT_TEST_SUITE_END();
};
@@ -98,6 +102,33 @@ void TestString::testDecimalStringToNumber()
CPPUNIT_ASSERT_EQUAL((sal_uInt32)81, comphelper::string::decimalStringToNumber(s1));
}
+void TestString::testIsdigitAsciiString()
+{
+ rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("1234"));
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s1), true);
+
+ rtl::OString s2(RTL_CONSTASCII_STRINGPARAM("1A34"));
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s2), false);
+
+ rtl::OString s3;
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3), true);
+}
+
+void TestString::testIsalnumAsciiString()
+{
+ rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("1234"));
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s1), true);
+
+ rtl::OString s2(RTL_CONSTASCII_STRINGPARAM("1A34"));
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s2), true);
+
+ rtl::OString s3;
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s3), true);
+
+ rtl::OString s4(RTL_CONSTASCII_STRINGPARAM("1A[4"));
+ CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s4), false);
+}
+
using namespace ::com::sun::star;
class testCollator : public cppu::WeakImplHelper1< i18n::XCollator >
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx
index b31d11549815..0138cc672ed8 100644
--- a/comphelper/source/misc/string.cxx
+++ b/comphelper/source/misc/string.cxx
@@ -354,25 +354,45 @@ NaturalStringSorter::NaturalStringSorter(
namespace
{
- template <typename T> bool tmpl_isAsciiDecimalString(const T &rString)
+ template <typename T> bool tmpl_isalnumAsciiString(const T &rString)
{
for (sal_Int32 i = 0; i < rString.getLength(); ++i)
{
- if ((rString[i] < '0') || (rString[i] > '9'))
+ if (!isalnumAscii(rString[i]))
return false;
}
return true;
}
+
+ template <typename T> bool tmpl_isdigitAsciiString(const T &rString)
+ {
+ for (sal_Int32 i = 0; i < rString.getLength(); ++i)
+ {
+ if (!isdigitAscii(rString[i]))
+ return false;
+ }
+ return true;
+ }
+}
+
+bool isalnumAsciiString(const rtl::OString &rString)
+{
+ return tmpl_isalnumAsciiString(rString);
+}
+
+bool isalnumAsciiString(const rtl::OUString &rString)
+{
+ return tmpl_isalnumAsciiString(rString);
}
-bool isAsciiDecimalString(const rtl::OString &rString)
+bool isdigitAsciiString(const rtl::OString &rString)
{
- return tmpl_isAsciiDecimalString(rString);
+ return tmpl_isdigitAsciiString(rString);
}
-bool isAsciiDecimalString(const rtl::OUString &rString)
+bool isdigitAsciiString(const rtl::OUString &rString)
{
- return tmpl_isAsciiDecimalString(rString);
+ return tmpl_isdigitAsciiString(rString);
}
} }