Explorar o código

Extended String class

- added "padRight" method to fill string with specific character on the right side
- added "padLeft" method to fill string with specific character on the left side
- extended tests for String class
pcmattulat %!s(int64=4) %!d(string=hai) anos
pai
achega
01b5bfe8d0
Modificáronse 3 ficheiros con 63 adicións e 3 borrados
  1. 30 1
      source/boxing/String.cpp
  2. 6 1
      source/boxing/String.hpp
  3. 27 1
      test/cases/boxing/StringTest.cpp

+ 30 - 1
source/boxing/String.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-16
+ * Changed:         2020-08-21
  *
  * */
 
@@ -106,6 +106,14 @@ bool ls_std::String::equalsIgnoreCase(std::string _text) {
   return this->toLowerCase() == ls_std::String{std::move(_text)}.toLowerCase();
 }
 
+std::string ls_std::String::padLeft(size_t _width, const char _fillCharacter) {
+  return ls_std::String::_createFillContent(this->value, _width, _fillCharacter) + this->value;
+}
+
+std::string ls_std::String::padRight(size_t _width, const char _fillCharacter) {
+  return this->value + ls_std::String::_createFillContent(this->value, _width, _fillCharacter);
+}
+
 std::string ls_std::String::reverse() {
   std::string copy = this->value;
   std::reverse(copy.begin(), copy.end());
@@ -130,3 +138,24 @@ std::string ls_std::String::toUpperCase() {
 
   return copy;
 }
+
+std::string ls_std::String::_buildCharacterChain(size_t _amount, const char _fillCharacter) {
+  std::string fillContent {};
+
+  for(size_t iteration {} ; iteration < _amount ; iteration++) {
+    fillContent += _fillCharacter;
+  }
+
+  return fillContent;
+}
+
+std::string ls_std::String::_createFillContent(const std::string& _text, size_t _width, const char _fillCharacter) {
+  size_t fillSize = _text.size() > _width ? 0 : _width - _text.size();
+  std::string fillContent {};
+
+  if(fillSize > 0) {
+    fillContent = ls_std::String::_buildCharacterChain(fillSize, _fillCharacter);
+  }
+
+  return fillContent;
+}

+ 6 - 1
source/boxing/String.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-19
+ * Changed:         2020-08-21
  *
  * */
 
@@ -63,6 +63,8 @@ namespace ls_std {
       bool endsWith(const std::string& _text);
       bool equalsIgnoreCase(String _string);
       bool equalsIgnoreCase(std::string _text);
+      std::string padLeft(size_t _width, const char _fillCharacter);
+      std::string padRight(size_t _width, const char _fillCharacter);
       std::string reverse();
       bool startsWith(const std::string& _text);
       std::string toLowerCase();
@@ -71,6 +73,9 @@ namespace ls_std {
     private:
 
       std::string value {};
+
+      static std::string _buildCharacterChain(size_t _amount, const char _fillCharacter);
+      static std::string _createFillContent(const std::string& _text, size_t _width, const char _fillCharacter);
   };
 }
 

+ 27 - 1
test/cases/boxing/StringTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-14
- * Changed:         2020-08-14
+ * Changed:         2020-08-21
  *
  * */
 
@@ -149,6 +149,32 @@ namespace {
     ASSERT_FALSE(text.endsWith("efg"));
   }
 
+  TEST_F(StringTest, padLeft)
+  {
+    ls_std::String text {"abcdef"};
+    ls_std::String anotherText {"ab"};
+    ls_std::String emptyText {};
+    ls_std::String longText {"This text is too long to fill!"};
+
+    ASSERT_STREQ("    abcdef", text.padLeft(10, ' ').c_str());
+    ASSERT_STREQ("        ab", anotherText.padLeft(10, ' ').c_str());
+    ASSERT_STREQ("          ", emptyText.padLeft(10, ' ').c_str());
+    ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
+  }
+
+  TEST_F(StringTest, padRight)
+  {
+    ls_std::String text {"abcdef"};
+    ls_std::String anotherText {"ab"};
+    ls_std::String emptyText {};
+    ls_std::String longText {"This text is too long to fill!"};
+
+    ASSERT_STREQ("abcdef    ", text.padRight(10, ' ').c_str());
+    ASSERT_STREQ("ab        ", anotherText.padRight(10, ' ').c_str());
+    ASSERT_STREQ("          ", emptyText.padRight(10, ' ').c_str());
+    ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
+  }
+
   TEST_F(StringTest, reverse)
   {
     ls_std::String text {};