string compare and tolower

pull/16/head
eduard 12 years ago
parent 4e4d15f8ac
commit 8545667bbd

@ -41,30 +41,6 @@
namespace
{
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
bool stringCompareNoCase (std::string first, std::string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
if (first.length()<second.length())
return true;
else
return false;
}
template<typename T1, typename T2>
bool selectCompare (char comp, T1 value1, T2 value2)
@ -72,7 +48,7 @@ namespace
switch (comp)
{
case '0': return value1==value2;
case '1': return value1!=value2;
// case '1': return value1!=value2;
case '2': return value1>value2;
case '3': return value1>=value2;
case '4': return value1<value2;
@ -149,7 +125,7 @@ namespace
//helper function
std::string::size_type find_str_ci(const std::string& str, const std::string& substr,size_t pos)
{
return toLower(str).find(toLower(substr),pos);
return Misc::toLower(str).find(Misc::toLower(substr),pos);
}
}
@ -204,7 +180,7 @@ namespace MWDialogue
if(!NPCstats.getFactionRanks().empty())
{
std::string NPCFaction = NPCstats.getFactionRanks().begin()->first;
if(PCstats.getFactionRanks().find(toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1;
if(PCstats.getFactionRanks().find(Misc::toLower(NPCFaction)) != PCstats.getFactionRanks().end()) sameFaction = 1;
}
if(!selectCompare<int,int>(comp,sameFaction,select.mI)) return false;
}
@ -307,12 +283,12 @@ namespace MWDialogue
if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int ||
select.mType==ESM::VT_Long)
{
if (!checkGlobal (comp, toLower (name), select.mI))
if (!checkGlobal (comp, Misc::toLower (name), select.mI))
return false;
}
else if (select.mType==ESM::VT_Float)
{
if (!checkGlobal (comp, toLower (name), select.mF))
if (!checkGlobal (comp, Misc::toLower (name), select.mF))
return false;
}
else
@ -326,13 +302,13 @@ namespace MWDialogue
if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int ||
select.mType==ESM::VT_Long)
{
if (!checkLocal (comp, toLower (name), select.mI, actor,
if (!checkLocal (comp, Misc::toLower (name), select.mI, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else if (select.mType==ESM::VT_Float)
{
if (!checkLocal (comp, toLower (name), select.mF, actor,
if (!checkLocal (comp, Misc::toLower (name), select.mF, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
@ -345,7 +321,7 @@ namespace MWDialogue
case '4'://journal
if(select.mType==ESM::VT_Int)
{
if(!selectCompare<int,int>(comp,MWBase::Environment::get().getJournal()->getJournalIndex(toLower(name)),select.mI)) return false;
if(!selectCompare<int,int>(comp,MWBase::Environment::get().getJournal()->getJournalIndex(Misc::toLower(name)),select.mI)) return false;
}
else
throw std::runtime_error (
@ -361,7 +337,7 @@ namespace MWDialogue
int sum = 0;
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter)
if (toLower(iter->getCellRef().mRefID) == toLower(name))
if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(name))
sum += iter->getRefData().getCount();
if(!selectCompare<int,int>(comp,sum,select.mI)) return false;
}
@ -375,7 +351,7 @@ namespace MWDialogue
case '7':// not ID
if(select.mType==ESM::VT_String ||select.mType==ESM::VT_Int)//bug in morrowind here? it's not a short, it's a string
{
int isID = int(toLower(name)==toLower(MWWorld::Class::get (actor).getId (actor)));
int isID = int(Misc::toLower(name)==Misc::toLower(MWWorld::Class::get (actor).getId (actor)));
if (selectCompare<int,int>(comp,!isID,select.mI)) return false;
}
else
@ -391,7 +367,7 @@ namespace MWDialogue
if(select.mType==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isFaction = int(toLower(npc->base->mFaction) == toLower(name));
int isFaction = int(Misc::toLower(npc->base->mFaction) == Misc::toLower(name));
if(selectCompare<int,int>(comp,!isFaction,select.mI))
return false;
}
@ -408,7 +384,7 @@ namespace MWDialogue
if(select.mType==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isClass = int(toLower(npc->base->mClass) == toLower(name));
int isClass = int(Misc::toLower(npc->base->mClass) == Misc::toLower(name));
if(selectCompare<int,int>(comp,!isClass,select.mI))
return false;
}
@ -425,7 +401,7 @@ namespace MWDialogue
if(select.mType==ESM::VT_Int)
{
MWWorld::LiveCellRef<ESM::NPC>* npc = actor.get<ESM::NPC>();
int isRace = int(toLower(npc->base->mRace) == toLower(name));
int isRace = int(Misc::toLower(npc->base->mRace) == Misc::toLower(name));
if(selectCompare<int,int>(comp,!isRace,select.mI))
return false;
}
@ -438,7 +414,7 @@ namespace MWDialogue
case 'B'://not Cell
if(select.mType==ESM::VT_Int)
{
int isCell = int(toLower(actor.getCell()->cell->mName) == toLower(name));
int isCell = int(Misc::toLower(actor.getCell()->cell->mName) == Misc::toLower(name));
if(selectCompare<int,int>(comp,!isCell,select.mI))
return false;
}
@ -451,13 +427,13 @@ namespace MWDialogue
if (select.mType==ESM::VT_Short || select.mType==ESM::VT_Int ||
select.mType==ESM::VT_Long)
{
if (checkLocal (comp, toLower (name), select.mI, actor,
if (checkLocal (comp, Misc::toLower (name), select.mI, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
else if (select.mType==ESM::VT_Float)
{
if (checkLocal (comp, toLower (name), select.mF, actor,
if (checkLocal (comp, Misc::toLower (name), select.mF, actor,
MWBase::Environment::get().getWorld()->getStore()))
return false;
}
@ -482,7 +458,7 @@ namespace MWDialogue
// actor id
if (!info.mActor.empty())
if (toLower (info.mActor)!=MWWorld::Class::get (actor).getId (actor))
if (Misc::toLower (info.mActor)!=MWWorld::Class::get (actor).getId (actor))
return false;
//NPC race
@ -496,7 +472,7 @@ namespace MWDialogue
if (!cellRef)
return false;
if (toLower (info.mRace)!=toLower (cellRef->base->mRace))
if (Misc::toLower (info.mRace)!=Misc::toLower (cellRef->base->mRace))
return false;
}
@ -511,7 +487,7 @@ namespace MWDialogue
if (!cellRef)
return false;
if (toLower (info.mClass)!=toLower (cellRef->base->mClass))
if (Misc::toLower (info.mClass)!=Misc::toLower (cellRef->base->mClass))
return false;
}
@ -523,7 +499,7 @@ namespace MWDialogue
//MWWorld::Class npcClass = MWWorld::Class::get(actor);
MWMechanics::NpcStats stats = MWWorld::Class::get(actor).getNpcStats(actor);
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.mNpcFaction));
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(Misc::toLower(info.mNpcFaction));
if(it!=stats.getFactionRanks().end())
{
//check rank
@ -540,7 +516,7 @@ namespace MWDialogue
if(!info.mPcFaction.empty())
{
MWMechanics::NpcStats stats = MWWorld::Class::get(MWBase::Environment::get().getWorld()->getPlayer().getPlayer()).getNpcStats(MWBase::Environment::get().getWorld()->getPlayer().getPlayer());
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(toLower(info.mPcFaction));
std::map<std::string,int>::iterator it = stats.getFactionRanks().find(Misc::toLower(info.mPcFaction));
if(it!=stats.getFactionRanks().end())
{
//check rank
@ -593,13 +569,13 @@ namespace MWDialogue
ESMS::RecListCaseT<ESM::Dialogue>::MapType dialogueList = MWBase::Environment::get().getWorld()->getStore().dialogs.list;
for(ESMS::RecListCaseT<ESM::Dialogue>::MapType::iterator it = dialogueList.begin(); it!=dialogueList.end();it++)
{
mDialogueMap[toLower(it->first)] = it->second;
mDialogueMap[Misc::toLower(it->first)] = it->second;
}
}
void DialogueManager::addTopic (const std::string& topic)
{
mKnownTopics[toLower(topic)] = true;
mKnownTopics[Misc::toLower(topic)] = true;
}
void DialogueManager::parseText (std::string text)
@ -753,9 +729,9 @@ namespace MWDialogue
{
if (isMatching (mActor, *iter) && functionFilter(mActor,*iter,true))
{
mActorKnownTopics.push_back(toLower(it->first));
mActorKnownTopics.push_back(Misc::toLower(it->first));
//does the player know the topic?
if(mKnownTopics.find(toLower(it->first)) != mKnownTopics.end())
if(mKnownTopics.find(Misc::toLower(it->first)) != mKnownTopics.end())
{
keywordList.push_back(it->first);
break;
@ -813,7 +789,7 @@ namespace MWDialogue
win->setServices (windowServices);
// sort again, because the previous sort was case-sensitive
keywordList.sort(stringCompareNoCase);
keywordList.sort(Misc::stringCompareNoCase);
win->setKeywords(keywordList);
mChoice = choice;
@ -907,7 +883,7 @@ namespace MWDialogue
{
MWGui::DialogueWindow* win = MWBase::Environment::get().getWindowManager()->getDialogueWindow();
win->askQuestion(question);
mChoiceMap[toLower(question)] = choice;
mChoiceMap[Misc::toLower(question)] = choice;
mIsInChoice = true;
}

@ -24,19 +24,6 @@
#include "scrollwindow.hpp"
#include "spellwindow.hpp"
namespace
{
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
}
namespace MWGui
{
@ -284,7 +271,7 @@ namespace MWGui
for (MWWorld::ContainerStoreIterator it = invStore.begin();
it != invStore.end(); ++it)
{
if (toLower(it->getCellRef().mRefID) == "gold_001")
if (Misc::toLower(it->getCellRef().mRefID) == "gold_001")
return it->getRefData().getCount();
}
return 0;

@ -18,19 +18,6 @@
#include "interpretercontext.hpp"
#include "ref.hpp"
namespace
{
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
}
namespace MWScript
{
namespace Container
@ -78,7 +65,7 @@ namespace MWScript
Interpreter::Type_Integer sum = 0;
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter)
if (toLower(iter->getCellRef().mRefID) == toLower(item))
if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(item))
sum += iter->getRefData().getCount();
runtime.push (sum);
@ -108,7 +95,7 @@ namespace MWScript
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end() && count;
++iter)
{
if (toLower(iter->getCellRef().mRefID) == toLower(item))
if (Misc::toLower(iter->getCellRef().mRefID) == Misc::toLower(item))
{
if (iter->getRefData().getCount()<=count)
{

@ -34,12 +34,6 @@ namespace
return sum;
}
bool compare_string_ci(std::string str1, std::string str2)
{
boost::algorithm::to_lower(str1);
return str1 == str2;
}
}
MWWorld::ContainerStore::ContainerStore() : mStateId (0), mCachedWeight (0), mWeightUpToDate (false) {}
@ -81,11 +75,11 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& ptr)
MWWorld::LiveCellRef<ESM::Miscellaneous> *gold =
ptr.get<ESM::Miscellaneous>();
if (compare_string_ci(gold->ref.mRefID, "gold_001")
|| compare_string_ci(gold->ref.mRefID, "gold_005")
|| compare_string_ci(gold->ref.mRefID, "gold_010")
|| compare_string_ci(gold->ref.mRefID, "gold_025")
|| compare_string_ci(gold->ref.mRefID, "gold_100"))
if (Misc::compare_string_ci(gold->ref.mRefID, "gold_001")
|| Misc::compare_string_ci(gold->ref.mRefID, "gold_005")
|| Misc::compare_string_ci(gold->ref.mRefID, "gold_010")
|| Misc::compare_string_ci(gold->ref.mRefID, "gold_025")
|| Misc::compare_string_ci(gold->ref.mRefID, "gold_100"))
{
MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), "Gold_001");
@ -93,7 +87,7 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& ptr)
ref.getPtr().getRefData().setCount(count);
for (MWWorld::ContainerStoreIterator iter (begin(type)); iter!=end(); ++iter)
{
if (compare_string_ci((*iter).get<ESM::Miscellaneous>()->ref.mRefID, "gold_001"))
if (Misc::compare_string_ci((*iter).get<ESM::Miscellaneous>()->ref.mRefID, "gold_001"))
{
(*iter).getRefData().setCount( (*iter).getRefData().getCount() + count);
flagAsModified();

@ -228,12 +228,12 @@ namespace MWWorld
return cell;
// didn't work -> now check for regions
std::string cellName2 = ESMS::RecListT<ESM::Region>::toLower (cellName);
std::string cellName2 = Misc::toLower (cellName);
for (ESMS::RecListT<ESM::Region>::MapType::const_iterator iter (mStore.regions.list.begin());
iter!=mStore.regions.list.end(); ++iter)
{
if (ESMS::RecListT<ESM::Region>::toLower (iter->second.mName)==cellName2)
if (Misc::toLower (iter->second.mName)==cellName2)
{
if (const ESM::Cell *cell = mStore.cells.searchExtByRegion (iter->first))
return cell;
@ -563,16 +563,6 @@ namespace MWWorld
}
}
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
void World::moveObject(const Ptr &ptr, CellStore &newCell, float x, float y, float z)
{
ESM::Position &pos = ptr.getRefData().getPosition();
@ -585,7 +575,7 @@ namespace MWWorld
if (*currCell != newCell) {
if (isPlayer) {
if (!newCell.isExterior()) {
changeToInteriorCell(toLower(newCell.cell->mName), pos);
changeToInteriorCell(Misc::toLower(newCell.cell->mName), pos);
} else {
int cellX = newCell.cell->mData.mX;
int cellY = newCell.cell->mData.mY;

@ -28,15 +28,6 @@ namespace ESMS
virtual int getSize() = 0;
virtual void listIdentifier (std::vector<std::string>& identifier) const = 0;
static std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
};
typedef std::map<int,RecList*> RecListList;
@ -53,14 +44,14 @@ namespace ESMS
// Load one object of this type
void load(ESMReader &esm, const std::string &id)
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
list[id2].load(esm);
}
// Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
typename MapType::const_iterator iter = list.find (id2);
@ -104,7 +95,7 @@ namespace ESMS
// Load one object of this type
void load(ESMReader &esm, const std::string &id)
{
//std::string id2 = toLower (id);
//std::string id2 = Misc::toLower (id);
list[id].load(esm);
}
@ -112,12 +103,12 @@ namespace ESMS
// Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
for (typename MapType::const_iterator iter = list.begin();
iter != list.end(); ++iter)
{
if (toLower(iter->first) == id2)
if (Misc::toLower(iter->first) == id2)
return &iter->second;
}
@ -127,12 +118,12 @@ namespace ESMS
// non-const version
X* search(const std::string &id)
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
for (typename MapType::iterator iter = list.begin();
iter != list.end(); ++iter)
{
if (toLower(iter->first) == id2)
if (Misc::toLower(iter->first) == id2)
return &iter->second;
}
@ -172,7 +163,7 @@ namespace ESMS
// Load one object of this type
void load(ESMReader &esm, const std::string &id)
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
list[id2].mId = id2;
list[id2].load(esm);
}
@ -180,7 +171,7 @@ namespace ESMS
// Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
typename MapType::const_iterator iter = list.find (id2);
@ -223,7 +214,7 @@ namespace ESMS
void load(ESMReader &esm, const std::string &id)
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
X& ref = list[id2];
ref.mId = id;
@ -233,7 +224,7 @@ namespace ESMS
// Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
typename MapType::const_iterator iter = list.find (id2);
@ -440,7 +431,7 @@ namespace ESMS
{
for (ExtCells::const_iterator iter = extCells.begin(); iter!=extCells.end(); ++iter)
{
if (toLower (iter->second->mName) == toLower (id))
if (Misc::toLower (iter->second->mName) == Misc::toLower (id))
return iter->second;
}
@ -449,10 +440,10 @@ namespace ESMS
const ESM::Cell *searchExtByRegion (const std::string& id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
for (ExtCells::const_iterator iter = extCells.begin(); iter!=extCells.end(); ++iter)
if (toLower (iter->second->mRegion)==id)
if (Misc::toLower (iter->second->mRegion)==id)
return iter->second;
return 0;
@ -586,7 +577,7 @@ namespace ESMS
X ref;
ref.load (esm);
std::string realId = toLower (ref.mData.mName.toString());
std::string realId = Misc::toLower (ref.mData.mName.toString());
std::swap (list[realId], ref);
}
@ -594,7 +585,7 @@ namespace ESMS
// Find the given object ID, or return NULL if not found.
const X* search(const std::string &id) const
{
std::string id2 = toLower (id);
std::string id2 = Misc::toLower (id);
typename MapType::const_iterator iter = list.find (id2);

@ -1,7 +1,14 @@
#include "stringops.hpp"
#include <cctype>
#include <algorithm>
#include <iterator>
#include <string.h>
#include <libs/platform/strings.h>
#include <boost/algorithm/string.hpp>
namespace Misc
{
@ -61,4 +68,34 @@ bool iends(const char* str1, const char* str2)
return strcasecmp(str2, str1+len1-len2) == 0;
}
std::string toLower (const std::string& name)
{
std::string lowerCase;
std::transform (name.begin(), name.end(), std::back_inserter (lowerCase),
(int(*)(int)) std::tolower);
return lowerCase;
}
bool stringCompareNoCase (std::string first, std::string second)
{
unsigned int i=0;
while ( (i<first.length()) && (i<second.length()) )
{
if (tolower(first[i])<tolower(second[i])) return true;
else if (tolower(first[i])>tolower(second[i])) return false;
++i;
}
if (first.length()<second.length())
return true;
else
return false;
}
bool compare_string_ci(std::string str1, std::string str2)
{
boost::algorithm::to_lower(str1);
return str1 == str2;
}
}

@ -1,6 +1,8 @@
#ifndef MISC_STRINGOPS_H
#define MISC_STRINGOPS_H
#include <string>
namespace Misc
{
@ -16,6 +18,13 @@ bool ibegins(const char* str1, const char* str2);
/// Case insensitive, returns true if str1 ends with substring str2
bool iends(const char* str1, const char* str2);
std::string toLower (const std::string& name);
bool stringCompareNoCase (std::string first, std::string second);
bool compare_string_ci (std::string first, std::string second);
}
#endif

Loading…
Cancel
Save