forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.cpp
More file actions
55 lines (46 loc) · 1.36 KB
/
stringutils.cpp
File metadata and controls
55 lines (46 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// stringutils.cpp
//
// Copyright (C) 2001-present, the Celestia Development Team
// Original version by Chris Laurel <claurel@shatters.net>
//
// Miscellaneous useful functions.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <cctype>
#include "stringutils.h"
using namespace std;
int compareIgnoringCase(string_view s1, string_view s2)
{
auto i1 = s1.begin();
auto i2 = s2.begin();
while (i1 != s1.end() && i2 != s2.end())
{
if (toupper(*i1) != toupper(*i2))
return (toupper(*i1) < toupper(*i2)) ? -1 : 1;
++i1;
++i2;
}
return s2.size() - s1.size();
}
int compareIgnoringCase(string_view s1, string_view s2, int n)
{
auto i1 = s1.begin();
auto i2 = s2.begin();
while (i1 != s1.end() && i2 != s2.end() && n > 0)
{
if (toupper(*i1) != toupper(*i2))
return (toupper(*i1) < toupper(*i2)) ? -1 : 1;
++i1;
++i2;
n--;
}
return n > 0 ? s2.size() - s1.size() : 0;
}
bool CompareIgnoringCasePredicate::operator()(string_view s1,
string_view s2) const
{
return compareIgnoringCase(s1, s2) < 0;
}