Refactored string operations

Conflicts:
	src/lib/base/String.cpp
This commit is contained in:
Xinyu Hou 2015-04-02 14:01:50 +01:00 committed by XinyuHou
parent cb0f0dd06d
commit 39e183da3e
3 changed files with 50 additions and 11 deletions

View File

@ -27,6 +27,9 @@
#include <algorithm> #include <algorithm>
#include <stdio.h> #include <stdio.h>
#include <cstdarg> #include <cstdarg>
#include <sstream>
#include <iomanip>
#include <algorithm>
namespace synergy { namespace synergy {
namespace string { namespace string {
@ -180,6 +183,30 @@ removeFileExt(String filename)
return filename.substr(0, dot); return filename.substr(0, dot);
} }
void
toHex(CString& subject, int width, const char fill)
{
std::stringstream ss;
ss << std::hex;
for (unsigned int i = 0; i < subject.length(); i++) {
ss << std::setw(width) << std::setfill(fill) << (int)(unsigned char)subject[i];
}
subject = ss.str();
}
void
uppercase(CString& subject)
{
std::transform(subject.begin(), subject.end(), subject.begin(), ::toupper);
}
void
removeChar(CString& subject, const char c)
{
subject.erase(std::remove(subject.begin(), subject.end(), c), subject.end());
}
// //
// CaselessCmp // CaselessCmp
// //

View File

@ -70,6 +70,25 @@ Finds the last dot and remove all characters from the dot to the end
*/ */
String removeFileExt(String filename); String removeFileExt(String filename);
//! Convert into hexdecimal
/*!
Convert each character in \c subject into hexdecimal form with \c width
*/
void toHex(CString& subject, int width, const char fill = '0');
//! Convert to all uppercase
/*!
Convert each character in \c subject to uppercase
*/
void uppercase(CString& subject);
//! Remove all specific char in suject
/*!
Remove all specific \c char in \c suject
*/
void removeChar(CString& subject, const char c);
//! Case-insensitive comparisons //! Case-insensitive comparisons
/*! /*!
This class provides case-insensitve comparison functions. This class provides case-insensitve comparison functions.

View File

@ -28,10 +28,7 @@
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <memory> #include <memory>
#include <sstream>
#include <iomanip>
#include <fstream> #include <fstream>
#include <algorithm>
// //
// SecureSocket // SecureSocket
@ -419,15 +416,11 @@ CSecureSocket::verifyCertFingerprint()
} }
// convert fingerprint into hexdecimal format // convert fingerprint into hexdecimal format
std::stringstream ss; CString fingerprint(reinterpret_cast<char*>(tempFingerprint), tempFingerprintLen);
ss << std::hex; synergy::string::toHex(fingerprint, 2);
for (unsigned int i = 0; i < tempFingerprintLen; i++) {
ss << std::setw(2) << std::setfill('0') << (int)tempFingerprint[i];
}
// all uppercase // all uppercase
CString fingerprint = ss.str(); synergy::string::uppercase(fingerprint);
std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(), ::toupper);
// check if this fingerprint exist // check if this fingerprint exist
CString fileLine; CString fileLine;
@ -445,7 +438,7 @@ CSecureSocket::verifyCertFingerprint()
if (!certificateFingerprint.empty()) { if (!certificateFingerprint.empty()) {
// remove colons // remove colons
certificateFingerprint.erase(std::remove(certificateFingerprint.begin(), certificateFingerprint.end(), ':'), certificateFingerprint.end()); synergy::string::removeChar(certificateFingerprint, ':');
if(certificateFingerprint.compare(fingerprint) == 0) { if(certificateFingerprint.compare(fingerprint) == 0) {
file.close(); file.close();