String String::Erase(const int pos, int len) { int str_len = Size(); int len_a = pos; // If erase after the length of the string, return. if (len_a >= str_len) return *this; // Prevent erasing after the end of the string. if ((len_a + len) > str_len) { // Resize the erase length. len = str_len - len_a; } String s; int len_b = str_len - len_a - len; s.Allocate(len_a + len_b + 1); Copy(&s.v[0], &v[0], len_a); if (len_b > 0) { int offset_b = len_a + len; Concatenate(&s.v[0], &v[0 + offset_b]); } s.v[s.Size()] = 0; v = s.v; return *this; }
TEST(String, Clear) { String s = "abc"; EXPECT_FALSE(s.IsEmpty()); EXPECT_EQ(3, s.Length()); EXPECT_EQ(3, s.Size()); EXPECT_EQ("abc", s); s.Clear(); EXPECT_TRUE(s.IsEmpty()); EXPECT_EQ(0, s.Length()); EXPECT_EQ(0, s.Size()); EXPECT_EQ("", s); }
/*============================================================================= -- Returns true if the open element (thing in tags "<>" but with a "/") is equivalent to the token. =============================================================================*/ bool HMLFile::AtElementOpen(String element) { //add the "<>" tags if they aren't already there if (element[0] != "<") element.Insert("<", 0); if (element[element.Size()-1] != ">") element.Insert(">", element.Size()); if (GetToken() == element) return true; if (mFile.eof()) return true; return false; }
void ColladaLoader::LoadDAEModel( const char* ModelName, const uint ProgramID) { ColladaDoc colladadoc; VModelData mesh; String ModelPath = FindDAEModelPath(ModelName); if (ModelPath.Size() > 1) { colladadoc.Read((ModelPath + ModelName + ".dae").Val()); mesh.SetModelPath(ModelPath.Val()); mesh.SetName(ModelName); mesh.LoadColladaDoc(colladadoc); LoadModel(mesh); LogMessage("Model '%s' loaded", ModelName); } else { LogError("File '%s.dae' not found", ModelName); } }
const String& GetLocaleUnix() { static String globalLocale; if (globalLocale.Size() == 0) globalLocale = String::UnsafeStringCreation( setlocale(LC_ALL, 0), RF_Core::DataManagment::TransfereOwnership); return globalLocale; }
String operator+(const String& LH, const String &RH) { if (LH.Length() + RH.Length()) { Size bytes = LH.Size() + RH.Size() - 1; String str(bytes); if (LH.Length() > 0) RF_SysMem::Copy(const_cast<char*>(str.c_str()), LH.c_str(), LH.Size()); if (RH.Length() > 0) RF_SysMem::Copy(const_cast<char*>(str.c_str() + LH.Size() - 1), RH.c_str(), RH.Size()); return str; } return String(); }
String String::Erase(const int pos) { String s; s.Allocate(pos + 1); Copy(&s.v[0], &v[0], pos); s.v[s.Size()] = 0; v = s.v; return *this; }
String operator+(const String& LH, const Char &RH) { Size bytes = LH.Size() + 1; String str(bytes); if (LH.Length() > 0) RF_SysMem::Copy(const_cast<char*>(str.c_str()), LH.c_str(), bytes - 2); str[str.Length()-1] = RH; str[str.Length()]='\0'; return str; }
/*============================================================================= -- Seeks through the file until it reaches the opening element (thing in tags "<>") or the end. =============================================================================*/ bool HMLFile::SeekElementOpen(String element) { //add the "<>" tags if they aren't already there if (element[0] != "<") element.Insert("<", 0); if (element[element.Size()-1] != ">") element.Insert(">", element.Size()); if (!mFile.is_open()) return false; //seek to the element tag while (GetToken() != element.GetStd()) { //return false because the file couldn't seek any further (at end of file) if (!Seek()) return false; } return true; }
/*! * Finds the last occurrence of a string * * \throws ExceptionDomain index out of bounds * * \param[in] s the string to search * \param[in] last_pos the position of the beginning, NPos for the end. * \return the index of the first occurrence, or NPos if not found. */ size_t String::BackwardFind(const String &s, size_t last_pos) const { if (s.Size() > Size()) return NPos(); if (last_pos == NPos()) return data.rfind(s.data); if (last_pos >= Size()) { throw ExceptionDomain(StringUTF8("int String::BackwardFind(const String &s, int last_pos = -1) const: ") + _("index out of bounds.")); } return data.rfind(s.data, last_pos); }
/*! * Edit distance * * \author Wikibooks * \param[in] s the string to compare * \return the amount of suppression, addition and change to morph from one string to the other */ int String::EditDistance(const String &s) const { unsigned int s1 = (unsigned int)Size(); unsigned int s2 = (unsigned int)s.Size(); std::vector<std::vector<unsigned int> > d(s1 + 1, std::vector<unsigned int>(s2 + 1)); for (unsigned int i = 1; i <= s1; ++i) d[i][0] = i; for (unsigned int i = 1; i <= s2; ++i) d[0][i] = i; for (unsigned int i = 1; i <= s1; ++i) for (unsigned int j = 1; j <= s2; ++j) d[i][j] = std::min(std::min(d[i - 1][j] + 1, d[i][j - 1] + 1), d[i - 1][j - 1] + (data[i - 1] == s[j - 1] ? 0 : 1)); return d[s1][s2]; }
/*! * Finds the first occurrence of a string * * \throws ExceptionDomain index out of bounds * * \param[in] s the string to search * \param[in] from_pos the position of the beginning * \return the index of the first occurrence, or NPos if not found. */ size_t String::Find(const String &s, size_t from_pos) const { if (IsEmpty()) return NPos(); if (s.Size() > Size()) { return NPos(); } if (from_pos >= Size()) { throw ExceptionDomain(StringUTF8("int String::Find(const String &s, int from_pos = 0) const: ") + _("index out of bounds.")); } return data.find(s.data, from_pos); }
void String::Insert( int idx, String const& s ) { Insert( idx, s.C_str(), s.Size() ); }
void String::Copy(String &d, const char *s, const int len) { for (int i = 0; i < len; ++i) d.v << s[i]; d.v[d.Size()] = 0; }
void ExternalProcess::Write( const String& text ) { if ( !text.IsEmpty() ) if ( (*API->ExternalProcess->WriteToExternalProcess)( handle, text.c_str(), text.Size() ) == api_false ) throw APIFunctionError( "WriteToExternalProcess" ); }