diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-02-14 18:57:18 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-02-14 21:36:32 (GMT) |
commit | cb05f5a908e20006c954ce38755c2e422ecc2388 (patch) | |
tree | a793551a5fe279a57d4330119560e8542f745484 /Swiften/Base | |
parent | cad974b45c0fb9355e68d9728e42c9ae3dbcebc7 (diff) | |
download | swift-cb05f5a908e20006c954ce38755c2e422ecc2388.zip swift-cb05f5a908e20006c954ce38755c2e422ecc2388.tar.bz2 |
Removed Swift::String.
Diffstat (limited to 'Swiften/Base')
-rw-r--r-- | Swiften/Base/ByteArray.cpp | 4 | ||||
-rw-r--r-- | Swiften/Base/ByteArray.h | 10 | ||||
-rw-r--r-- | Swiften/Base/IDGenerator.cpp | 10 | ||||
-rw-r--r-- | Swiften/Base/IDGenerator.h | 6 | ||||
-rw-r--r-- | Swiften/Base/Paths.cpp | 6 | ||||
-rw-r--r-- | Swiften/Base/SConscript | 2 | ||||
-rw-r--r-- | Swiften/Base/String.cpp | 40 | ||||
-rw-r--r-- | Swiften/Base/String.h | 139 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/IDGeneratorTest.cpp | 4 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/StringTest.cpp | 126 |
10 files changed, 85 insertions, 262 deletions
diff --git a/Swiften/Base/ByteArray.cpp b/Swiften/Base/ByteArray.cpp index 36cc19c..7701268 100644 --- a/Swiften/Base/ByteArray.cpp +++ b/Swiften/Base/ByteArray.cpp @@ -26,8 +26,8 @@ namespace Swift { static const int BUFFER_SIZE = 4096; -void ByteArray::readFromFile(const String& file) { - std::ifstream input(file.getUTF8Data(), std::ios_base::in|std::ios_base::binary); +void ByteArray::readFromFile(const std::string& file) { + std::ifstream input(file.c_str(), std::ios_base::in|std::ios_base::binary); while (input.good()) { size_t oldSize = data_.size(); data_.resize(oldSize + BUFFER_SIZE); diff --git a/Swiften/Base/ByteArray.h b/Swiften/Base/ByteArray.h index b5cbfb0..90a4907 100644 --- a/Swiften/Base/ByteArray.h +++ b/Swiften/Base/ByteArray.h @@ -10,7 +10,7 @@ #include <vector> #include <iostream> -#include "Swiften/Base/String.h" +#include <string> namespace Swift { class ByteArray @@ -20,7 +20,7 @@ namespace Swift { ByteArray() : data_() {} - ByteArray(const String& s) : data_(s.getUTF8String().begin(), s.getUTF8String().end()) {} + ByteArray(const std::string& s) : data_(s.begin(), s.end()) {} ByteArray(const char* c) { while (*c) { @@ -107,11 +107,11 @@ namespace Swift { return data_.end(); } - String toString() const { - return String(getData(), getSize()); + std::string toString() const { + return std::string(getData(), getSize()); } - void readFromFile(const String& file); + void readFromFile(const std::string& file); void clear() { data_.clear(); diff --git a/Swiften/Base/IDGenerator.cpp b/Swiften/Base/IDGenerator.cpp index b620de7..74a0f65 100644 --- a/Swiften/Base/IDGenerator.cpp +++ b/Swiften/Base/IDGenerator.cpp @@ -11,16 +11,16 @@ namespace Swift { IDGenerator::IDGenerator() { } -String IDGenerator::generateID() { +std::string IDGenerator::generateID() { bool carry = true; size_t i = 0; - while (carry && i < currentID_.getUTF8Size()) { - char c = currentID_.getUTF8String()[i]; + while (carry && i < currentID_.size()) { + char c = currentID_[i]; if (c >= 'z') { - currentID_.getUTF8String()[i] = 'a'; + currentID_[i] = 'a'; } else { - currentID_.getUTF8String()[i] = c+1; + currentID_[i] = c+1; carry = false; } ++i; diff --git a/Swiften/Base/IDGenerator.h b/Swiften/Base/IDGenerator.h index 2089658..4b6289b 100644 --- a/Swiften/Base/IDGenerator.h +++ b/Swiften/Base/IDGenerator.h @@ -7,17 +7,17 @@ #ifndef SWIFTEN_IDGenerator_H #define SWIFTEN_IDGenerator_H -#include "Swiften/Base/String.h" +#include <string> namespace Swift { class IDGenerator { public: IDGenerator(); - String generateID(); + std::string generateID(); private: - String currentID_; + std::string currentID_; }; } diff --git a/Swiften/Base/Paths.cpp b/Swiften/Base/Paths.cpp index c1dbef2..0e69b61 100644 --- a/Swiften/Base/Paths.cpp +++ b/Swiften/Base/Paths.cpp @@ -25,7 +25,7 @@ boost::filesystem::path Paths::getExecutablePath() { uint32_t size = 4096; path.resize(size); if (_NSGetExecutablePath(path.getData(), &size) == 0) { - return boost::filesystem::path(path.toString().getUTF8Data()).parent_path(); + return boost::filesystem::path(path.toString().c_str()).parent_path(); } #elif defined(SWIFTEN_PLATFORM_LINUX) ByteArray path; @@ -33,13 +33,13 @@ boost::filesystem::path Paths::getExecutablePath() { size_t size = readlink("/proc/self/exe", path.getData(), path.getSize()); if (size > 0) { path.resize(size); - return boost::filesystem::path(path.toString().getUTF8Data()).parent_path(); + return boost::filesystem::path(path.toString().c_str()).parent_path(); } #elif defined(SWIFTEN_PLATFORM_WINDOWS) ByteArray data; data.resize(2048); GetModuleFileName(NULL, data.getData(), data.getSize()); - return boost::filesystem::path(data.toString().getUTF8Data()).parent_path(); + return boost::filesystem::path(data.toString().c_str()).parent_path(); #endif return boost::filesystem::path(); } diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript index 9c7b8dc..ca22044 100644 --- a/Swiften/Base/SConscript +++ b/Swiften/Base/SConscript @@ -5,8 +5,8 @@ objects = swiften_env.StaticObject([ "Error.cpp", "Log.cpp", "Paths.cpp", - "IDGenerator.cpp", "String.cpp", + "IDGenerator.cpp", "sleep.cpp", ]) swiften_env.Append(SWIFTEN_OBJECTS = [objects]) diff --git a/Swiften/Base/String.cpp b/Swiften/Base/String.cpp index 460df36..7ddf614 100644 --- a/Swiften/Base/String.cpp +++ b/Swiften/Base/String.cpp @@ -7,7 +7,7 @@ #include <cassert> #include <algorithm> -#include "Swiften/Base/String.h" +#include <Swiften/Base/String.h> namespace Swift { @@ -34,11 +34,11 @@ static inline size_t sequenceLength(char firstByte) { return 1; } -std::vector<unsigned int> String::getUnicodeCodePoints() const { +std::vector<unsigned int> String::getUnicodeCodePoints(const std::string& s) { std::vector<unsigned int> result; - for (size_t i = 0; i < data_.size();) { + for (size_t i = 0; i < s.size();) { unsigned int codePoint = 0; - char firstChar = data_[i]; + char firstChar = s[i]; size_t length = sequenceLength(firstChar); // First character is special @@ -49,7 +49,7 @@ std::vector<unsigned int> String::getUnicodeCodePoints() const { codePoint = firstChar & ((1<<(firstCharBitSize+1)) - 1); for (size_t j = 1; j < length; ++j) { - codePoint = (codePoint<<6) | (data_[i+j] & 0x3F); + codePoint = (codePoint<<6) | (s[i+j] & 0x3F); } result.push_back(codePoint); i += length; @@ -58,37 +58,37 @@ std::vector<unsigned int> String::getUnicodeCodePoints() const { } -std::pair<String,String> String::getSplittedAtFirst(char c) const { +std::pair<std::string,std::string> String::getSplittedAtFirst(const std::string& s, char c) { assert((c & 0x80) == 0); - size_t firstMatch = data_.find(c); - if (firstMatch != data_.npos) { - return std::make_pair(data_.substr(0,firstMatch),data_.substr(firstMatch+1,data_.npos)); + size_t firstMatch = s.find(c); + if (firstMatch != s.npos) { + return std::make_pair(s.substr(0,firstMatch),s.substr(firstMatch+1,s.npos)); } else { - return std::make_pair(*this, ""); + return std::make_pair(s, ""); } } -void String::replaceAll(char c, const String& s) { +void String::replaceAll(std::string& src, char c, const std::string& s) { size_t lastPos = 0; size_t matchingIndex = 0; - while ((matchingIndex = data_.find(c, lastPos)) != data_.npos) { - data_.replace(matchingIndex, 1, s.data_); - lastPos = matchingIndex + s.data_.size(); + while ((matchingIndex = src.find(c, lastPos)) != src.npos) { + src.replace(matchingIndex, 1, s); + lastPos = matchingIndex + s.size(); } } -std::vector<String> String::split(char c) const { +std::vector<std::string> String::split(const std::string& s, char c) { assert((c & 0x80) == 0); - std::vector<String> result; - String accumulator; - for (size_t i = 0; i < data_.size(); ++i) { - if (data_[i] == c) { + std::vector<std::string> result; + std::string accumulator; + for (size_t i = 0; i < s.size(); ++i) { + if (s[i] == c) { result.push_back(accumulator); accumulator = ""; } else { - accumulator += data_[i]; + accumulator += s[i]; } } result.push_back(accumulator); diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h index 7d2f928..192d53b 100644 --- a/Swiften/Base/String.h +++ b/Swiften/Base/String.h @@ -6,141 +6,24 @@ #pragma once -#include <boost/algorithm/string.hpp> - -#include <ostream> +#include <map> #include <string> -#include <utility> #include <vector> -#include <cassert> -#include <algorithm> #define SWIFTEN_STRING_TO_CFSTRING(a) \ - CFStringCreateWithBytes(NULL, reinterpret_cast<const UInt8*>(a.getUTF8Data()), a.getUTF8Size(), kCFStringEncodingUTF8, false) + CFStringCreateWithBytes(NULL, reinterpret_cast<const UInt8*>(a.c_str()), a.size(), kCFStringEncodingUTF8, false) namespace Swift { - class ByteArray; - - class String { - friend class ByteArray; - - public: - String() {} - String(const char* data) : data_(data) {} - String(const char* data, size_t len) : data_(data, len) {} - String(const std::string& data) : data_(data) {} - - bool isEmpty() const { return data_.empty(); } - - const char* getUTF8Data() const { return data_.c_str(); } - const std::string& getUTF8String() const { return data_; } - std::string& getUTF8String() { return data_; } - size_t getUTF8Size() const { return data_.size(); } - std::vector<unsigned int> getUnicodeCodePoints() const; - - void clear() { data_.clear(); } - - /** - * Returns the part before and after 'c'. - * If the given splitter does not occur in the string, the second - * component is the empty string. - */ - std::pair<String,String> getSplittedAtFirst(char c) const; - - std::vector<String> split(char c) const; - - String getLowerCase() const { - return boost::to_lower_copy(data_); - } - - void removeAll(char c) { - data_.erase(std::remove(data_.begin(), data_.end(), c), data_.end()); - } - - void replaceAll(char c, const String& s); - - bool beginsWith(char c) const { - return data_.size() > 0 && data_[0] == c; - } - - bool beginsWith(const String& s) const { - return boost::starts_with(data_, s.data_); - } - - bool endsWith(char c) const { - return data_.size() > 0 && data_[data_.size()-1] == c; - } - - bool endsWith(const String& s) const { - return boost::ends_with(data_, s.data_); - } - - String getSubstring(size_t begin, size_t end) const { - return String(data_.substr(begin, end)); - } - - size_t find(char c) const { - assert((c & 0x80) == 0); - return data_.find(c); - } - - size_t npos() const { - return data_.npos; - } - - friend String operator+(const String& a, const String& b) { - return String(a.data_ + b.data_); + namespace String { + std::vector<unsigned int> getUnicodeCodePoints(const std::string&); + std::pair<std::string, std::string> getSplittedAtFirst(const std::string&, char c); + std::vector<std::string> split(const std::string&, char c); + void replaceAll(std::string&, char c, const std::string& s); + inline bool beginsWith(const std::string& s, char c) { + return s.size() > 0 && s[0] == c; } - - friend String operator+(const String& a, char b) { - return String(a.data_ + b); + inline bool endsWith(const std::string& s, char c) { + return s.size() > 0 && s[s.size()-1] == c; } - - String& operator+=(const String& o) { - data_ += o.data_; - return *this; - } - - String& operator+=(char c) { - data_ += c; - return *this; - } - - String& operator=(const String& o) { - data_ = o.data_; - return *this; - } - - bool contains(const String& o) { - return data_.find(o.data_) != std::string::npos; - } - - char operator[](size_t i) const { - return data_[i]; - } - - friend bool operator>(const String& a, const String& b) { - return a.data_ > b.data_; - } - - friend bool operator<(const String& a, const String& b) { - return a.data_ < b.data_; - } - - friend bool operator!=(const String& a, const String& b) { - return a.data_ != b.data_; - } - - friend bool operator==(const String& a, const String& b) { - return a.data_ == b.data_; - } - - friend std::ostream& operator<<(std::ostream& os, const String& s) { - os << s.data_; - return os; - } - - private: - std::string data_; }; } diff --git a/Swiften/Base/UnitTest/IDGeneratorTest.cpp b/Swiften/Base/UnitTest/IDGeneratorTest.cpp index 4a6b29c..4874684 100644 --- a/Swiften/Base/UnitTest/IDGeneratorTest.cpp +++ b/Swiften/Base/UnitTest/IDGeneratorTest.cpp @@ -28,13 +28,13 @@ class IDGeneratorTest : public CppUnit::TestFixture void testGenerate() { IDGenerator testling; for (unsigned int i = 0; i < 26*4; ++i) { - String id = testling.generateID(); + std::string id = testling.generateID(); CPPUNIT_ASSERT(generatedIDs_.insert(id).second); } } private: - std::set<String> generatedIDs_; + std::set<std::string> generatedIDs_; }; CPPUNIT_TEST_SUITE_REGISTRATION(IDGeneratorTest); diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp index 161b4f1..884bbee 100644 --- a/Swiften/Base/UnitTest/StringTest.cpp +++ b/Swiften/Base/UnitTest/StringTest.cpp @@ -6,38 +6,29 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> +#include <string> -#include "Swiften/Base/String.h" +#include <Swiften/Base/String.h> using namespace Swift; -class StringTest : public CppUnit::TestFixture -{ +class StringTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(StringTest); CPPUNIT_TEST(testGetUnicodeCodePoints); CPPUNIT_TEST(testGetSplittedAtFirst); CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd); CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter); - CPPUNIT_TEST(testRemoveAll); - CPPUNIT_TEST(testRemoveAll_LastChar); - CPPUNIT_TEST(testRemoveAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll); CPPUNIT_TEST(testReplaceAll_LastChar); CPPUNIT_TEST(testReplaceAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll_MatchingReplace); - CPPUNIT_TEST(testGetLowerCase); CPPUNIT_TEST(testSplit); - CPPUNIT_TEST(testContains); - CPPUNIT_TEST(testContainsFalse); - CPPUNIT_TEST(testContainsExact); - CPPUNIT_TEST(testEndsWith); - CPPUNIT_TEST(testEndsWith_DoesNotEndWith); CPPUNIT_TEST_SUITE_END(); public: void testGetUnicodeCodePoints() { - String testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); - std::vector<unsigned int> points = testling.getUnicodeCodePoints(); + std::string testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); + std::vector<unsigned int> points = String::getUnicodeCodePoints(testling); CPPUNIT_ASSERT_EQUAL(0x24U, points[0]); CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]); @@ -46,120 +37,69 @@ class StringTest : public CppUnit::TestFixture } void testGetSplittedAtFirst() { - String testling("ab@cd@ef"); + std::string testling("ab@cd@ef"); - std::pair<String,String> result = testling.getSplittedAtFirst('@'); - CPPUNIT_ASSERT_EQUAL(String("ab"), result.first); - CPPUNIT_ASSERT_EQUAL(String("cd@ef"), result.second); + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT_EQUAL(std::string("cd@ef"), result.second); } void testGetSplittedAtFirst_CharacterAtEnd() { - String testling("ab@"); + std::string testling("ab@"); - std::pair<String,String> result = testling.getSplittedAtFirst('@'); - CPPUNIT_ASSERT_EQUAL(String("ab"), result.first); - CPPUNIT_ASSERT(result.second.isEmpty()); + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT(result.second.empty()); } void testGetSplittedAtFirst_NoSuchCharacter() { - String testling("ab"); + std::string testling("ab"); - std::pair<String,String> result = testling.getSplittedAtFirst('@'); - CPPUNIT_ASSERT_EQUAL(String("ab"), result.first); - CPPUNIT_ASSERT(result.second.isEmpty()); - } - - void testRemoveAll() { - String testling("ab c de"); - - testling.removeAll(' '); - - CPPUNIT_ASSERT_EQUAL(String("abcde"), testling); - } - - void testRemoveAll_LastChar() { - String testling("abcde "); - - testling.removeAll(' '); - - CPPUNIT_ASSERT_EQUAL(String("abcde"), testling); - } - - void testRemoveAll_ConsecutiveChars() { - String testling("ab cde"); - - testling.removeAll(' '); - - CPPUNIT_ASSERT_EQUAL(String("abcde"), testling); + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT(result.second.empty()); } void testReplaceAll() { - String testling("abcbd"); + std::string testling("abcbd"); - testling.replaceAll('b', "xyz"); + String::replaceAll(testling, 'b', "xyz"); - CPPUNIT_ASSERT_EQUAL(String("axyzcxyzd"), testling); + CPPUNIT_ASSERT_EQUAL(std::string("axyzcxyzd"), testling); } void testReplaceAll_LastChar() { - String testling("abc"); + std::string testling("abc"); - testling.replaceAll('c', "xyz"); + String::replaceAll(testling, 'c', "xyz"); - CPPUNIT_ASSERT_EQUAL(String("abxyz"), testling); + CPPUNIT_ASSERT_EQUAL(std::string("abxyz"), testling); } void testReplaceAll_ConsecutiveChars() { - String testling("abbc"); + std::string testling("abbc"); - testling.replaceAll('b',"xyz"); + String::replaceAll(testling, 'b',"xyz"); - CPPUNIT_ASSERT_EQUAL(String("axyzxyzc"), testling); + CPPUNIT_ASSERT_EQUAL(std::string("axyzxyzc"), testling); } void testReplaceAll_MatchingReplace() { - String testling("abc"); + std::string testling("abc"); - testling.replaceAll('b',"bbb"); + String::replaceAll(testling, 'b',"bbb"); - CPPUNIT_ASSERT_EQUAL(String("abbbc"), testling); - } - - void testGetLowerCase() { - String testling("aBcD e"); - - CPPUNIT_ASSERT_EQUAL(String("abcd e"), testling.getLowerCase()); + CPPUNIT_ASSERT_EQUAL(std::string("abbbc"), testling); } void testSplit() { - std::vector<String> result = String("abc def ghi").split(' '); + std::vector<std::string> result = String::split("abc def ghi", ' '); CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size())); - CPPUNIT_ASSERT_EQUAL(String("abc"), result[0]); - CPPUNIT_ASSERT_EQUAL(String("def"), result[1]); - CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]); - } - - void testContains() { - CPPUNIT_ASSERT(String("abcde").contains(String("bcd"))); + CPPUNIT_ASSERT_EQUAL(std::string("abc"), result[0]); + CPPUNIT_ASSERT_EQUAL(std::string("def"), result[1]); + CPPUNIT_ASSERT_EQUAL(std::string("ghi"), result[2]); } - - void testContainsFalse() { - CPPUNIT_ASSERT(!String("abcde").contains(String("abcdef"))); - } - - void testContainsExact() { - CPPUNIT_ASSERT(String("abcde").contains(String("abcde"))); - } - - void testEndsWith() { - CPPUNIT_ASSERT(String("abcdef").endsWith("cdef")); - } - - void testEndsWith_DoesNotEndWith() { - CPPUNIT_ASSERT(!String("abcdef").endsWith("ddef")); - } - }; CPPUNIT_TEST_SUITE_REGISTRATION(StringTest); |