Skip to content

Commit

Permalink
Merge pull request #143 from jpd236/across-down-canonicalization
Browse files Browse the repository at this point in the history
Improve handling of different capitalizations of "Across" and "Down".
  • Loading branch information
mrichards42 authored Jun 6, 2021
2 parents 0dbcfea + 25829d1 commit 1cb113e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions puz/Clue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,18 @@ class PUZ_API Clues : public std::vector<std::pair<string_t, ClueList> >

ClueList & SetClueList(const string_t & direction, const ClueList & cluelist)
{
operator[](direction) = cluelist;
ClueList & ret = operator[](direction);
// Normalize different capitalizations of "Across" and "Down", since these have special meaning.
// We still retain the original direction in the title field.
string_t canonical_direction;
if (CaseInsensitiveEquals(direction, puzT("Across")))
canonical_direction = puzT("Across");
else if (CaseInsensitiveEquals(direction, puzT("Down")))
canonical_direction = puzT("Down");
else
canonical_direction = direction;

operator[](canonical_direction) = cluelist;
ClueList & ret = operator[](canonical_direction);
if (ret.GetTitle().empty())
ret.SetTitle(direction);
return ret;
Expand Down
15 changes: 15 additions & 0 deletions puz/puzstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,21 @@ bool EndsWith(const string_t & str, const string_t & cmp)
str.compare(str.size() - cmp.size(), cmp.size(), cmp) == 0;
}

bool CaseInsensitiveEquals(const string_t& a, const string_t& b)
{
unsigned int sz = a.size();
if (b.size() != sz)
return false;
for (unsigned int i = 0; i < sz; ++i)
#if PUZ_UNICODE
if (towlower(a[i]) != towlower(b[i]))
#else
if (tolower(a[i]) != tolower(b[i]))
#endif
return false;
return true;
}

//----------------------------------------------------------------------------
// Convert between formatted / unformatted
//----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions puz/puzstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ int ToInt(const string_t & str);
bool StartsWith(const string_t & str, const string_t & cmp);
bool EndsWith(const string_t & str, const string_t & cmp);

bool CaseInsensitiveEquals(const string_t& a, const string_t& b);

// XML stuff
enum {
UNESCAPE_BR = 1,
Expand Down

0 comments on commit 1cb113e

Please sign in to comment.