Skip to content

Commit

Permalink
feat: Added string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
macmade committed Nov 7, 2024
1 parent e2d91c0 commit f94b5d3
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 0 deletions.
54 changes: 54 additions & 0 deletions SRPXX-Tests/String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2024 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

#include <SRPXX.hpp>
#include <XSTest/XSTest.hpp>

XSTest( String, ToLower )
{
XSTestAssertTrue( SRP::String::toLower( "abcd" ) == "abcd" );
XSTestAssertTrue( SRP::String::toLower( "ABCD" ) == "abcd" );
XSTestAssertTrue( SRP::String::toLower( "AbCd" ) == "abcd" );
XSTestAssertTrue( SRP::String::toLower( "1234" ) == "1234" );
XSTestAssertTrue( SRP::String::toLower( "A1B2" ) == "a1b2" );
}

XSTest( String, ToUpper )
{
XSTestAssertTrue( SRP::String::toUpper( "ABCD" ) == "ABCD" );
XSTestAssertTrue( SRP::String::toUpper( "abcd" ) == "ABCD" );
XSTestAssertTrue( SRP::String::toUpper( "AbCd" ) == "ABCD" );
XSTestAssertTrue( SRP::String::toUpper( "1234" ) == "1234" );
XSTestAssertTrue( SRP::String::toUpper( "a1b2" ) == "A1B2" );
}

XSTest( String, HasPrefix )
{
XSTestAssertTrue( SRP::String::hasPrefix( "abcd", "a" ) );
XSTestAssertTrue( SRP::String::hasPrefix( "abcd", "ab" ) );
XSTestAssertTrue( SRP::String::hasPrefix( "abcd", "abcd" ) );

XSTestAssertFalse( SRP::String::hasPrefix( "abcd", "b" ) );
XSTestAssertFalse( SRP::String::hasPrefix( "abcd", "abcde" ) );
}
56 changes: 56 additions & 0 deletions SRPXX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
050961382CDD50B4008F4C59 /* String.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 050961312CDD50B4008F4C59 /* String.hpp */; };
050961392CDD50B4008F4C59 /* SRPXX.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 050961332CDD50B4008F4C59 /* SRPXX.hpp */; };
0509613A2CDD50B4008F4C59 /* String.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 050961352CDD50B4008F4C59 /* String.cpp */; };
0509613D2CDD50C1008F4C59 /* String.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0509613B2CDD50C1008F4C59 /* String.cpp */; };
051F72C42CDD12600031F7FA /* libSRPXX.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 051F72B82CDD12480031F7FA /* libSRPXX.a */; };
051F72DE2CDD13790031F7FA /* XSTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 051F72CF2CDD12AB0031F7FA /* XSTest.framework */; };
/* End PBXBuildFile section */
Expand All @@ -29,6 +33,10 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
050961312CDD50B4008F4C59 /* String.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = String.hpp; sourceTree = "<group>"; };
050961332CDD50B4008F4C59 /* SRPXX.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = SRPXX.hpp; sourceTree = "<group>"; };
050961352CDD50B4008F4C59 /* String.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = String.cpp; sourceTree = "<group>"; };
0509613B2CDD50C1008F4C59 /* String.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = String.cpp; sourceTree = "<group>"; };
051F72622CDD11CE0031F7FA /* CODE_OF_CONDUCT.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CODE_OF_CONDUCT.md; sourceTree = "<group>"; };
051F72632CDD11CE0031F7FA /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
051F72642CDD11CE0031F7FA /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -103,12 +111,56 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
050961322CDD50B4008F4C59 /* SRPXX */ = {
isa = PBXGroup;
children = (
050961312CDD50B4008F4C59 /* String.hpp */,
);
path = SRPXX;
sourceTree = "<group>";
};
050961342CDD50B4008F4C59 /* include */ = {
isa = PBXGroup;
children = (
050961322CDD50B4008F4C59 /* SRPXX */,
050961332CDD50B4008F4C59 /* SRPXX.hpp */,
);
path = include;
sourceTree = "<group>";
};
050961362CDD50B4008F4C59 /* source */ = {
isa = PBXGroup;
children = (
050961352CDD50B4008F4C59 /* String.cpp */,
);
path = source;
sourceTree = "<group>";
};
050961372CDD50B4008F4C59 /* SRPXX */ = {
isa = PBXGroup;
children = (
050961342CDD50B4008F4C59 /* include */,
050961362CDD50B4008F4C59 /* source */,
);
path = SRPXX;
sourceTree = "<group>";
};
0509613C2CDD50C1008F4C59 /* SRPXX-Tests */ = {
isa = PBXGroup;
children = (
0509613B2CDD50C1008F4C59 /* String.cpp */,
);
path = "SRPXX-Tests";
sourceTree = "<group>";
};
051F72422CDD11920031F7FA = {
isa = PBXGroup;
children = (
051F72642CDD11CE0031F7FA /* README.md */,
051F72632CDD11CE0031F7FA /* LICENSE */,
051F72622CDD11CE0031F7FA /* CODE_OF_CONDUCT.md */,
050961372CDD50B4008F4C59 /* SRPXX */,
0509613C2CDD50C1008F4C59 /* SRPXX-Tests */,
051F72A22CDD11DC0031F7FA /* xcconfig */,
051F72652CDD11D50031F7FA /* XSTest.xcodeproj */,
051F72DD2CDD13790031F7FA /* Frameworks */,
Expand Down Expand Up @@ -273,6 +325,8 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
050961382CDD50B4008F4C59 /* String.hpp in Headers */,
050961392CDD50B4008F4C59 /* SRPXX.hpp in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -387,13 +441,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0509613A2CDD50B4008F4C59 /* String.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
051F72BC2CDD12600031F7FA /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0509613D2CDD50C1008F4C59 /* String.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
30 changes: 30 additions & 0 deletions SRPXX/include/SRPXX.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2024 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

#ifndef SRPXX_HPP
#define SRPXX_HPP

#include <SRPXX/String.hpp>

#endif /* SRPXX_HPP */
41 changes: 41 additions & 0 deletions SRPXX/include/SRPXX/String.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2024 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

#ifndef SRPXX_STRING_HPP
#define SRPXX_STRING_HPP

#include <string>

namespace SRP
{
namespace String
{
std::string toLower( const std::string & str );
std::string toUpper( const std::string & str );

bool hasPrefix( const std::string & str, const std::string & prefix );
}
}

#endif /* SRPXX_STRING_HPP */
59 changes: 59 additions & 0 deletions SRPXX/source/String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2024 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/

#include <SRPXX/String.hpp>

namespace SRP
{
namespace String
{
std::string toLower( const std::string & str )
{
std::string ret( str );

std::transform( ret.begin(), ret.end(), ret.begin(), ::tolower );

return ret;
}

std::string toUpper( const std::string & str )
{
std::string ret( str );

std::transform( ret.begin(), ret.end(), ret.begin(), ::toupper );

return ret;
}

bool hasPrefix( const std::string & str, const std::string & prefix )
{
if( str.length() < prefix.length() )
{
return false;
}

return str.substr( 0, prefix.length() ) == prefix;
}
}
}

0 comments on commit f94b5d3

Please sign in to comment.