Commit e5dbf8ff authored by Davis King's avatar Davis King

Added the strings_equal_ignore_case() functions.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402930
parent c123f0c1
......@@ -3,12 +3,12 @@
#ifndef DLIB_STRINg_
#define DLIB_STRINg_
#include "string_abstract.h"
#include "../algs.h"
#include <string>
#include <iostream>
#include "../error.h"
#include "../assert.h"
#include "string_abstract.h"
#include "../uintn.h"
#include <cctype>
#include <algorithm>
......@@ -57,6 +57,129 @@ namespace dlib
return temp;
}
// ----------------------------------------------------------------------------------------
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const std::basic_string<char,traits,alloc>& str2
)
{
if (str1.size() != str2.size())
return false;
for (typename std::basic_string<char,traits,alloc>::size_type i = 0; i < str1.size(); ++i)
{
if (std::tolower(str1[i]) != std::tolower(str2[i]))
return false;
}
return true;
}
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const char* str2
)
{
typename std::basic_string<char,traits,alloc>::size_type i;
for (i = 0; i < str1.size(); ++i)
{
// if we hit the end of str2 then the strings aren't the same length
if (str2[i] == '\0')
return false;
if (std::tolower(str1[i]) != std::tolower(str2[i]))
return false;
}
// This happens when str2 is longer than str1
if (str2[i] != '\0')
return false;
return true;
}
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const char* str1,
const std::basic_string<char,traits,alloc>& str2
)
{
return strings_equal_ignore_case(str2, str1);
}
// ----------------------------------------------------------------------------------------
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const std::basic_string<char,traits,alloc>& str2,
unsigned long num
)
{
if (str1.size() != str2.size() && (str1.size() < num || str2.size() < num))
return false;
for (typename std::basic_string<char,traits,alloc>::size_type i = 0; i < str1.size() && i < num; ++i)
{
if (std::tolower(str1[i]) != std::tolower(str2[i]))
return false;
}
return true;
}
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const char* str2,
unsigned long num
)
{
typename std::basic_string<char,traits,alloc>::size_type i;
for (i = 0; i < str1.size() && i < num; ++i)
{
// if we hit the end of str2 then the strings aren't the same length
if (str2[i] == '\0')
return false;
if (std::tolower(str1[i]) != std::tolower(str2[i]))
return false;
}
return true;
}
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const char* str1,
const std::basic_string<char,traits,alloc>& str2,
unsigned long num
)
{
return strings_equal_ignore_case(str2, str1, num);
}
// ----------------------------------------------------------------------------------------
class cast_to_string_error : public error
......
......@@ -156,6 +156,38 @@ namespace dlib
- #S[i] == std::toupper(str[i])
!*/
// ----------------------------------------------------------------------------------------
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const std::basic_string<char,traits,alloc>& str2
);
/*!
ensures
- returns tolower(str1) == tolower(str2)
!*/
// ----------------------------------------------------------------------------------------
template <
typename traits,
typename alloc
>
bool strings_equal_ignore_case (
const std::basic_string<char,traits,alloc>& str1,
const std::basic_string<char,traits,alloc>& str2,
unsigned long num
);
/*!
ensures
- returns tolower(str1.substr(0,num)) == tolower(str2.substr(0,num))
(i.e. only compares the first num characters)
!*/
// ----------------------------------------------------------------------------------------
template <
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment