/* * Copyright (c) 2001 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ /* //---------------------------------------------------------------------- // Source file: strutils.cpp // Written by: Cliff Green, 1997-2001 // Compiler: Metrowerks CodeWarrior Pro 6 // History: // Modified: 8/15/2001 // By: Cliff Green // Comments: See header file. //---------------------------------------------------------------------- */ #include "strutils.h" /* // strlen_alpha, strcpy_alpha: utility functions which provide strlen // and strcpy functionality but skip non-alphabetic characters */ int strlen_alpha (const char* s) { int len = 0; while (*s != 0) { if (isalpha(*s)) { ++len; } ++s; } return len; } char* strcpy_alpha (char* s1, const char* s2) { char* sav = s1; /* for consistency with strcpy */ while (*s2 != 0) { if (isalpha(*s2)) { *s1 = static_cast(tolower(*s2)); ++s1; } ++s2; } *s1 = 0; return sav; } #ifdef __cplusplus #include #include #include // for std::equal namespace Util { const std::string toLower (const std::string& str) { // using std::tolower; std::string lowerStr(str); for (std::string::size_type i (0); i < str.length(); ++i) { lowerStr[i] = static_cast(tolower(str[i])); } return lowerStr; } const std::string toLowerAlpha (const std::string& str) { // using std::tolower; // using std::isalpha; std::string newStr; newStr.reserve(str.length()); // final size should be smaller for (std::string::size_type i (0); i < str.length(); ++i) { if (isalpha(str[i])) { newStr += static_cast(tolower(str[i])); } } return newStr; } bool compareNoCase (const std::string& lhs, const std::string& rhs) { if (lhs.length() != rhs.length()) { return false; } return std::equal(lhs.begin(), lhs.end(), rhs.begin(), CompareNoCase()); } bool compareNoCaseAlpha (const std::string& lhs, const std::string& rhs) { return toLowerAlpha(lhs) == toLowerAlpha(rhs); } } // end namespace #endif // wrapper for C++ string functions