Skip to content

Commit cfacc22

Browse files
committed
(Re)implement split_left(), split_right()
Remove split_left(), split_right() taking a Delimiter object.
1 parent f273912 commit cfacc22

File tree

3 files changed

+74
-115
lines changed

3 files changed

+74
-115
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ The following table presents types, values and simplified, short prototypes of t
174174
| &nbsp; | string **join**(collection\<string_view\> vec, string_view sep) | string with elements of collection joined with given separator string |
175175
| &nbsp; | &nbsp; | &nbsp; |
176176
| **Separating** | vector\<string_view\> **split**(string_view sv, string_view set) | vector of string_view with elements of string separated by characters from given set |
177-
| &nbsp; | tuple\<string_view, string_view\> **split_left**(string_view sv, Delimiter delim) | tuple with head and tail string_view on given string as split at left by given delimiter |
178-
| &nbsp; | tuple\<string_view, string_view\> **split_right**(string_view sv, Delimiter delim) | tuple with head and tail string_view on given string as split at right by given delimiter |
179177
| &nbsp; | tuple\<string_view, string_view\> **split_left**(string_view sv, string_view set) | tuple with head and tail string_view on given string as split at left by characters in given set |
180178
| &nbsp; | tuple\<string_view, string_view\> **split_right**(string_view sv, string_view set) | tuple with head and tail string_view on given string as split at right by characters in given set |
181179

@@ -284,8 +282,8 @@ strip_right: string with characters in set removed from right of string [" \t\n"
284282
strip: string with characters in set removed from left and right of string [" \t\n"]
285283
join: string with strings from collection joined separated by given separator
286284
split: split string into vector of string_view given delimiter - literal_delimiter
287-
split_left: split string into two-element tuple given delimiter - forward - literal_delimiter
288-
split_right: split string into two-element tuple given delimiter - reverse - literal_delimiter[TODO]
285+
split_left: split string into two-element tuple given set of delimiter characters - forward
286+
split_right: split string into two-element tuple given set of delimiter characters - reverse
289287
compare: negative, zero or positive for lsh is less than, equal to or greater than rhs
290288
operator==(): true if lhs string is equal to rhs string
291289
operator!=(): true if lhs string is not equal to rhs string

include/nonstd/string.hpp

Lines changed: 61 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -767,74 +767,74 @@ string_nodiscard std::size_t find_first( std17::basic_string_view<CharT> text, S
767767

768768
// find_first()
769769

770-
#define string_MK_FIND_FIRST(CharT) \
771-
template< typename SeekT > \
772-
string_nodiscard std::size_t \
770+
#define string_MK_FIND_FIRST(CharT) \
771+
template< typename SeekT > \
772+
string_nodiscard std::size_t \
773773
find_first( std17::basic_string_view<CharT> text, SeekT const & seek ) string_noexcept \
774-
{ \
775-
return text.find( seek ); \
774+
{ \
775+
return text.find( seek ); \
776776
}
777777

778778
#if string_CPP17_000
779-
# define string_MK_FIND_FIRST_CHAR(CharT) \
780-
string_nodiscard inline std::size_t \
779+
# define string_MK_FIND_FIRST_CHAR(CharT) \
780+
string_nodiscard inline std::size_t \
781781
find_first( std17::basic_string_view<CharT> text, CharT seek ) string_noexcept \
782-
{ \
782+
{ \
783783
return find_first( text, std::basic_string<CharT>( &seek, &seek + 1 ) ); \
784784
}
785785
#else
786-
# define string_MK_FIND_FIRST_CHAR(CharT) \
787-
string_nodiscard inline std::size_t \
786+
# define string_MK_FIND_FIRST_CHAR(CharT) \
787+
string_nodiscard inline std::size_t \
788788
find_first( std17::basic_string_view<CharT> text, CharT seek ) string_noexcept \
789-
{ \
789+
{ \
790790
return find_first( text, std17::basic_string_view<CharT>( &seek, &seek + 1 ) ); \
791791
}
792792
#endif
793793

794794
// find_last()
795795

796-
#define string_MK_FIND_LAST(CharT) \
797-
template< typename SeekT > \
798-
string_nodiscard std::size_t \
796+
#define string_MK_FIND_LAST(CharT) \
797+
template< typename SeekT > \
798+
string_nodiscard std::size_t \
799799
find_last( std17::basic_string_view<CharT> text, SeekT const & seek ) \
800-
{ \
801-
return text.rfind( seek ); \
800+
{ \
801+
return text.rfind( seek ); \
802802
}
803803

804804
#if string_CPP17_000
805-
# define string_MK_FIND_LAST_CHAR(CharT) \
806-
string_nodiscard inline std::size_t \
805+
# define string_MK_FIND_LAST_CHAR(CharT) \
806+
string_nodiscard inline std::size_t \
807807
find_last( std17::basic_string_view<CharT> text, CharT seek ) \
808-
{ \
808+
{ \
809809
return find_last( text, std::basic_string<CharT>( &seek, &seek + 1 ) ); \
810810
}
811811
#else
812-
# define string_MK_FIND_LAST_CHAR(CharT) \
813-
string_nodiscard inline std::size_t \
812+
# define string_MK_FIND_LAST_CHAR(CharT) \
813+
string_nodiscard inline std::size_t \
814814
find_last( std17::basic_string_view<CharT> text, CharT seek ) \
815-
{ \
815+
{ \
816816
return find_last( text, std17::basic_string_view<CharT>( &seek, &seek + 1 ) ); \
817817
}
818818
#endif
819819

820820
// find_first_of()
821821

822-
#define string_MK_FIND_FIRST_OF(CharT) \
823-
template< typename SeekT > \
824-
string_nodiscard std::size_t \
822+
#define string_MK_FIND_FIRST_OF(CharT) \
823+
template< typename SeekT > \
824+
string_nodiscard std::size_t \
825825
find_first_of( std17::basic_string_view<CharT> text, SeekT const & seek ) \
826-
{ \
827-
return text.find_first_of( seek ); \
826+
{ \
827+
return text.find_first_of( seek ); \
828828
}
829829

830830
// find_last_of()
831831

832-
#define string_MK_FIND_LAST_OF(CharT) \
833-
template< typename SeekT > \
834-
string_nodiscard std::size_t \
832+
#define string_MK_FIND_LAST_OF(CharT) \
833+
template< typename SeekT > \
834+
string_nodiscard std::size_t \
835835
find_last_of( std17::basic_string_view<CharT> text, SeekT const & seek ) \
836-
{ \
837-
return text.find_last_of( seek ); \
836+
{ \
837+
return text.find_last_of( seek ); \
838838
}
839839

840840
// find_first_not_of()
@@ -1886,54 +1886,27 @@ split( std17::basic_string_view<CharT> text, Delimiter delimiter, std::size_t Ns
18861886

18871887
// split_left -> tuple
18881888

1889-
#define string_MK_SPLIT_LEFT_DELIM( CharT ) \
1890-
template< typename Delimiter > \
1891-
string_nodiscard auto \
1892-
split_left( std17::basic_string_view<CharT> text, Delimiter delimiter ) \
1893-
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>> \
1894-
{ \
1895-
auto const result = split( text, delimiter, 2 ); \
1896-
\
1897-
return { result[0], result[1] }; \
1898-
}
1899-
1900-
#define string_MK_SPLIT_LEFT_STRING( CharT ) \
1901-
string_nodiscard inline auto \
1902-
split_left( std17::basic_string_view<CharT> text, std17::basic_string_view<CharT> set ) \
1903-
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>> \
1904-
{ \
1905-
return split_left( text, basic_literal_delimiter<CharT>(set) ); \
1906-
}
1907-
1908-
// TODO: split_right -> tuple
1909-
1910-
using CharT = char; // TODO: remove; temporary while developing routines.
1911-
1912-
#define string_MK_SPLIT_RIGHT_DELIM( CharT )
1913-
1914-
template< typename Delimiter >
1915-
string_nodiscard auto
1916-
split_right( std17::basic_string_view<CharT> /*text*/, Delimiter /*delimiter*/ )
1917-
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>>
1918-
{
1919-
return { "TODO", "TODO" };
1920-
1921-
// auto const result = split( text, delimiter, 2 );
1922-
//
1923-
// return { result[0], result[1] };
1889+
#define string_MK_SPLIT_LEFT( CharT ) \
1890+
string_nodiscard inline auto \
1891+
split_left( std17::basic_string_view<CharT> text, std17::basic_string_view<CharT> set ) \
1892+
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>> \
1893+
{ \
1894+
auto const pos = find_first_of( text, set ); \
1895+
\
1896+
return { text.substr( 0, pos ), text.substr( pos + 1 ) }; \
19241897
}
19251898

19261899
// Split string at given separator character, starting at right.
19271900

1928-
#define string_MK_SPLIT_RIGHT_STRING( CharT )
1929-
1930-
string_nodiscard inline auto
1931-
split_right( std17::basic_string_view<CharT> text, std17::basic_string_view<CharT> set )
1932-
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>>
1933-
{
1934-
return split_right( text, basic_literal_delimiter<CharT>(set) );
1935-
// return split_right( text, basic_reverse_literal_delimiter<CharT>(set) );
1936-
}
1901+
#define string_MK_SPLIT_RIGHT( CharT ) \
1902+
string_nodiscard inline auto \
1903+
split_right( std17::basic_string_view<CharT> text, std17::basic_string_view<CharT> set ) \
1904+
-> std::tuple<std17::basic_string_view<CharT>, std17::basic_string_view<CharT>> \
1905+
{ \
1906+
auto const pos = find_last_of( text, set ); \
1907+
\
1908+
return { text.substr( 0, pos ), text.substr( pos + 1 ) }; \
1909+
}
19371910

19381911
#endif // string_CONFIG_PROVIDE_CHAR_T
19391912

@@ -2047,10 +2020,8 @@ string_MK_CAPITALIZE ( char )
20472020
string_MK_JOIN ( char )
20482021
string_MK_SPLIT_DELIM ( char )
20492022
string_MK_SPLIT_STRING ( char )
2050-
string_MK_SPLIT_LEFT_DELIM ( char )
2051-
string_MK_SPLIT_LEFT_STRING ( char )
2052-
// string_MK_SPLIT_RIGHT_DELIM ( char )
2053-
// string_MK_SPLIT_RIGHT_STRING( char )
2023+
string_MK_SPLIT_LEFT ( char )
2024+
string_MK_SPLIT_RIGHT ( char )
20542025

20552026
string_MK_COMPARE ( char )
20562027

@@ -2118,10 +2089,8 @@ string_MK_CAPITALIZE ( wchar_t )
21182089
string_MK_JOIN ( wchar_t )
21192090
string_MK_SPLIT_DELIM ( wchar_t )
21202091
string_MK_SPLIT_STRING ( wchar_t )
2121-
string_MK_SPLIT_LEFT_DELIM ( wchar_t )
2122-
string_MK_SPLIT_LEFT_STRING ( wchar_t )
2123-
// string_MK_SPLIT_RIGHT_DELIM ( wchar_t )
2124-
// string_MK_SPLIT_RIGHT_STRING( wchar_t )
2092+
string_MK_SPLIT_LEFT ( wchar_t )
2093+
string_MK_SPLIT_RIGHT ( wchar_t )
21252094
// ...
21262095
string_MK_COMPARE ( wchar_t )
21272096

@@ -2189,10 +2158,8 @@ string_MK_CAPITALIZE ( char8_t )
21892158
string_MK_JOIN ( char8_t )
21902159
string_MK_SPLIT_DELIM ( char8_t )
21912160
string_MK_SPLIT_STRING ( char8_t )
2192-
string_MK_SPLIT_LEFT_DELIM ( char8_t )
2193-
string_MK_SPLIT_LEFT_STRING ( char8_t )
2194-
// string_MK_SPLIT_RIGHT_DELIM ( char8_t )
2195-
// string_MK_SPLIT_RIGHT_STRING( char8_t )
2161+
string_MK_SPLIT_LEFT ( char8_t )
2162+
string_MK_SPLIT_RIGHT ( char8_t )
21962163
// ...
21972164
string_MK_COMPARE ( char8_t )
21982165

@@ -2260,10 +2227,8 @@ string_MK_CAPITALIZE ( char16_t )
22602227
string_MK_JOIN ( char16_t )
22612228
string_MK_SPLIT_DELIM ( char16_t )
22622229
string_MK_SPLIT_STRING ( char16_t )
2263-
string_MK_SPLIT_LEFT_DELIM ( char16_t )
2264-
string_MK_SPLIT_LEFT_STRING ( char16_t )
2265-
// string_MK_SPLIT_RIGHT_DELIM ( char16_t )
2266-
// string_MK_SPLIT_RIGHT_STRING( char16_t )
2230+
string_MK_SPLIT_LEFT ( char16_t )
2231+
string_MK_SPLIT_RIGHT ( char16_t )
22672232
// ...
22682233
string_MK_COMPARE ( char16_t )
22692234

@@ -2331,10 +2296,8 @@ string_MK_CAPITALIZE ( char32_t )
23312296
string_MK_JOIN ( char32_t )
23322297
string_MK_SPLIT_DELIM ( char32_t )
23332298
string_MK_SPLIT_STRING ( char32_t )
2334-
string_MK_SPLIT_LEFT_DELIM ( char32_t )
2335-
string_MK_SPLIT_LEFT_STRING ( char32_t )
2336-
// string_MK_SPLIT_RIGHT_DELIM ( char32_t )
2337-
// string_MK_SPLIT_RIGHT_STRING( char32_t )
2299+
string_MK_SPLIT_LEFT ( char32_t )
2300+
string_MK_SPLIT_RIGHT ( char32_t )
23382301
// ...
23392302
string_MK_COMPARE ( char32_t )
23402303

@@ -2402,10 +2365,8 @@ string_MK_COMPARE_GT ( char32_t )
24022365
#undef string_MK_JOIN
24032366
#undef string_MK_SPLIT_DELIM
24042367
#undef string_MK_SPLIT_STRING
2405-
#undef string_MK_SPLIT_LEFT_DELIM
2406-
#undef string_MK_SPLIT_LEFT_STRING
2407-
// #undef string_MK_SPLIT_RIGHT_DELIM
2408-
// #undef string_MK_SPLIT_RIGHT_STRING
2368+
#undef string_MK_SPLIT_LEFT
2369+
#undef string_MK_SPLIT_RIGHT
24092370
#undef string_MK_COMPARE
24102371
#undef string_MK_COMPARE_EQ
24112372
#undef string_MK_COMPARE_NE

test/string.t.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -764,28 +764,28 @@ CASE( "split: split string into single characters given empty delimiter" )
764764

765765
// split_left()
766766

767-
CASE( "split_left: split string into two-element tuple given delimiter - forward - literal_delimiter" )
767+
CASE( "split_left: split string into two-element tuple given set of delimiter characters - forward" )
768768
{
769-
std17::string_view a, b;
770-
std::tie(a, b) = split_left("abc;def;ghi", ";");
769+
// std17::string_view a, b;
770+
// std::tie(a, b) = split_left("abc;def;ghi", ";");
771771

772772
// std::cout << "[a:" << a << "][b:" << b << "]\n";
773773

774-
EXPECT( split_left("abc;def;ghi", literal_delimiter(";")) == (std::tuple<std17::string_view, std17::string_view>("abc", "def;ghi")) );
775-
EXPECT( split_left("abc;def;ghi", ";" ) == (std::tuple<std17::string_view, std17::string_view>("abc", "def;ghi")) );
774+
EXPECT( split_left("abc;def;ghi", ";" ) == (std::tuple<std17::string_view, std17::string_view>("abc", "def;ghi" )) );
775+
EXPECT( split_left("abc;def;ghi", "/" ) == (std::tuple<std17::string_view, std17::string_view>("abc;def;ghi", "")) );
776776
}
777777

778778
// TODO: split_right()
779779

780-
CASE( "split_right: split string into two-element tuple given delimiter - reverse - literal_delimiter" "[TODO]" )
780+
CASE( "split_right: split string into two-element tuple given set of delimiter characters - reverse" )
781781
{
782-
std17::string_view a, b;
783-
std::tie(a, b) = split_right("abc;def;ghi", ";");
782+
// std17::string_view a, b;
783+
// std::tie(a, b) = split_right("abc;def;ghi", ";");
784784

785-
std::cout << "[a:" << a << "][b:" << b << "]\n";
785+
// std::cout << "[a:" << a << "][b:" << b << "]\n";
786786

787-
// EXPECT( split_right("abc;def;ghi", literal_delimiter(";")) == (std::tuple<std17::string_view, std17::string_view>("abc;def", "ghi")) );
788-
// EXPECT( split_right("abc;def;ghi", ";" ) == (std::tuple<std17::string_view, std17::string_view>("abc;def", "ghi")) );
787+
EXPECT( split_right("abc;def;ghi", ";" ) == (std::tuple<std17::string_view, std17::string_view>("abc;def", "ghi" )) );
788+
EXPECT( split_right("abc;def;ghi", "/" ) == (std::tuple<std17::string_view, std17::string_view>( "", "abc;def;ghi")) );
789789
}
790790

791791
// compare()

0 commit comments

Comments
 (0)