added: to convert/text
wchar_t ToLower(wchar_t c) wchar_t ToUpper(wchar_t c) void ToLower(std::wstring & s) void ToUpper(std::wstring & s) template<class StringType1, class StringType2> bool EqualNoCase(const StringType1 * str1, const StringType2 * str2) template<class StringType1, class StringType2> bool EqualNoCase(const StringType1 & str1, const StringType2 & str2) template<class StringType1, class StringType2> bool EqualNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2) added: to space: Value * GetValueNoCase(const wchar_t * name); Value * GetValueNoCase(const std::wstring & name); const Value * GetValueNoCase(const wchar_t * name) const; const Value * GetValueNoCase(const std::wstring & name) const; git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1111 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
parent
72a510ce71
commit
040eb12c8b
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017, Tomasz Sowa
|
||||
* Copyright (c) 2017-2018, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -153,6 +153,43 @@ bool IsDigit(wchar_t c, int base, int * digit)
|
|||
|
||||
|
||||
|
||||
wchar_t ToLower(wchar_t c)
|
||||
{
|
||||
if( c >= 'A' && c <= 'Z' )
|
||||
return c - 'A' + 'a';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
wchar_t ToUpper(wchar_t c)
|
||||
{
|
||||
if( c >= 'a' && c <= 'z' )
|
||||
return c - 'a' + 'A';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
void ToLower(std::wstring & s)
|
||||
{
|
||||
std::wstring::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToLower(s[i]);
|
||||
}
|
||||
|
||||
|
||||
void ToUpper(std::wstring & s)
|
||||
{
|
||||
std::wstring::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToUpper(s[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017, Tomasz Sowa
|
||||
* Copyright (c) 2017-2018, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -38,6 +38,8 @@
|
|||
#ifndef headerfile_picotools_convert_text
|
||||
#define headerfile_picotools_convert_text
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
|
@ -95,6 +97,57 @@ CharType * SkipWhiteFromBack(CharType * str, bool check_additional_chars = true,
|
|||
}
|
||||
|
||||
|
||||
wchar_t ToLower(wchar_t c);
|
||||
wchar_t ToUpper(wchar_t c);
|
||||
|
||||
|
||||
// change to a template
|
||||
void ToLower(std::wstring & s);
|
||||
void ToUpper(std::wstring & s);
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
while( *str1 && *str2 && ToLower(*str1) == ToLower(*str2) )
|
||||
{
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( *str1 == 0 && *str2 == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 & str1, const StringType2 & str2)
|
||||
{
|
||||
return EqualNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2)
|
||||
{
|
||||
while( str1_begin < str1_end && *str2 && PT::ToLower(*str1_begin) == PT::ToLower(*str2) )
|
||||
{
|
||||
++str1_begin;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( str1_begin == str1_end && *str2 == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
102
space/space.cpp
102
space/space.cpp
|
@ -149,6 +149,53 @@ const Space::Value * Space::GetValue(const std::wstring & name) const
|
|||
|
||||
|
||||
|
||||
Space::Value * Space::GetValueNoCase(const wchar_t * name)
|
||||
{
|
||||
tmp_name = name;
|
||||
return GetValueNoCase(tmp_name);
|
||||
}
|
||||
|
||||
|
||||
Space::Value * Space::GetValueNoCase(const std::wstring & name)
|
||||
{
|
||||
Table::iterator t = table.begin();
|
||||
|
||||
for( ; t != table.end() ; ++t)
|
||||
{
|
||||
if( PT::EqualNoCase(t->first, name) )
|
||||
{
|
||||
return &t->second;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const Space::Value * Space::GetValueNoCase(const wchar_t * name) const
|
||||
{
|
||||
tmp_name = name;
|
||||
return GetValueNoCase(tmp_name);
|
||||
}
|
||||
|
||||
|
||||
const Space::Value * Space::GetValueNoCase(const std::wstring & name) const
|
||||
{
|
||||
Table::const_iterator t = table.begin();
|
||||
|
||||
for( ; t != table.end() ; ++t)
|
||||
{
|
||||
if( PT::EqualNoCase(t->first, name) )
|
||||
{
|
||||
return &t->second;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::wstring * Space::GetFirstValue(const wchar_t * name)
|
||||
{
|
||||
|
@ -461,9 +508,9 @@ bool Space::ToBool(const std::wstring & value)
|
|||
const wchar_t * str_begin = SkipWhite(value.c_str());
|
||||
const wchar_t * str_end = SkipWhiteFromBack(str_begin);
|
||||
|
||||
return (EqualNoCase(str_begin, str_end, L"true") ||
|
||||
EqualNoCase(str_begin, str_end, L"yes") ||
|
||||
EqualNoCase(str_begin, str_end, L"1")
|
||||
return (PT::EqualNoCase(str_begin, str_end, L"true") ||
|
||||
PT::EqualNoCase(str_begin, str_end, L"yes") ||
|
||||
PT::EqualNoCase(str_begin, str_end, L"1")
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1007,55 +1054,6 @@ bool Space::ListText(const wchar_t * name, std::vector<std::wstring> & list)
|
|||
|
||||
|
||||
|
||||
wchar_t Space::ToSmall(wchar_t c)
|
||||
{
|
||||
if( c >= 'A' && c <= 'Z' )
|
||||
c = c - 'A' + 'a';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// IMPROVE ME
|
||||
// move me to a better place
|
||||
bool Space::EqualNoCase(const wchar_t * str1, const wchar_t * str2)
|
||||
{
|
||||
while( *str1 && *str2 && ToSmall(*str1) == ToSmall(*str2) )
|
||||
{
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( *str1 == 0 && *str2 == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* the first string is trimmed
|
||||
* str1_end is pointing one character after the last valid character
|
||||
*
|
||||
*/
|
||||
// IMPROVE ME
|
||||
// move me to a better place
|
||||
bool Space::EqualNoCase(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2)
|
||||
{
|
||||
while( str1_begin < str1_end && *str2 && ToSmall(*str1_begin) == ToSmall(*str2) )
|
||||
{
|
||||
++str1_begin;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( str1_begin == str1_end && *str2 == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
those white characters here should be the same as in spaceparser.cpp
|
||||
*/
|
||||
|
|
|
@ -214,11 +214,18 @@ public:
|
|||
*/
|
||||
Value * GetValue(const wchar_t * name);
|
||||
Value * GetValue(const std::wstring & name);
|
||||
|
||||
const Value * GetValue(const wchar_t * name) const;
|
||||
const Value * GetValue(const std::wstring & name) const;
|
||||
|
||||
|
||||
|
||||
// O(n) complexity
|
||||
Value * GetValueNoCase(const wchar_t * name);
|
||||
Value * GetValueNoCase(const std::wstring & name);
|
||||
const Value * GetValueNoCase(const wchar_t * name) const;
|
||||
const Value * GetValueNoCase(const std::wstring & name) const;
|
||||
|
||||
|
||||
// they can return a null pointer if there is not such a 'name'
|
||||
std::wstring * GetFirstValue(const wchar_t * name);
|
||||
std::wstring * GetFirstValue(const std::wstring & name);
|
||||
|
@ -406,11 +413,8 @@ private:
|
|||
static size_t ToSize(const std::wstring & value);
|
||||
static bool ToBool(const std::wstring & value);
|
||||
|
||||
static wchar_t ToSmall(wchar_t c);
|
||||
static bool EqualNoCase(const wchar_t * str1, const wchar_t * str2);
|
||||
static bool EqualNoCase(const wchar_t * str1_begin, const wchar_t * str1_end, const wchar_t * str2);
|
||||
static bool IsWhite(int c);
|
||||
static bool HasWhite(const std::wstring & str);
|
||||
static bool IsWhite(int c);
|
||||
static bool HasWhite(const std::wstring & str);
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue