Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 5b4c598

Browse files
committed
[[ Foundation ]] Simplify BeginsWith/EndsWith APIs
The MCStringBeginsWith and MCStringEndsWith APIs now have optional return parameters for the haystack match length. This removes the need for 'R' variants previously introduced.
1 parent 402409e commit 5b4c598

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

engine/src/exec-strings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ void MCStringsEvalBeginsWith(MCExecContext& ctxt, MCStringRef p_whole, MCStringR
15571557

15581558
bool t_found;
15591559
uindex_t t_self_length;
1560-
t_found = MCStringBeginsWithR(p_whole, p_part, t_compare_option, t_self_length);
1560+
t_found = MCStringBeginsWith(p_whole, p_part, t_compare_option, &t_self_length);
15611561
if (!t_found)
15621562
{
15631563
r_result = false;
@@ -1576,7 +1576,7 @@ void MCStringsEvalEndsWith(MCExecContext& ctxt, MCStringRef p_whole, MCStringRef
15761576

15771577
bool t_found;
15781578
uindex_t t_self_length;
1579-
t_found = MCStringEndsWithR(p_whole, p_part, t_compare_option, t_self_length);
1579+
t_found = MCStringEndsWith(p_whole, p_part, t_compare_option, &t_self_length);
15801580
if (!t_found)
15811581
{
15821582
r_result = false;

libfoundation/include/foundation-unicode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,12 @@ int32_t MCUnicodeCompare(const void *p_first, uindex_t p_first_length, bool p_fi
346346
// Returns whether the first string begins with the second
347347
bool MCUnicodeBeginsWith(const void *p_first, uindex_t p_first_length, bool p_first_native,
348348
const void *p_second, uindex_t p_second_length, bool p_second_native,
349-
MCUnicodeCompareOption, uindex_t& r_first_match_length);
349+
MCUnicodeCompareOption, uindex_t *r_first_match_length);
350350

351351
// Returns whether the first string ends with the second
352352
bool MCUnicodeEndsWith(const void *p_first, uindex_t p_first_length, bool p_first_native,
353353
const void *p_second, uindex_t p_second_length, bool p_second_native,
354-
MCUnicodeCompareOption, uindex_t& r_first_match_length);
354+
MCUnicodeCompareOption, uindex_t *r_first_match_length);
355355

356356
// Returns whether the string contains the given substring
357357
bool MCUnicodeContains(const void *p_string, uindex_t p_string_length, bool p_string_native,

libfoundation/include/foundation.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,16 +2259,20 @@ MC_DLLEXPORT compare_t MCStringCompareTo(MCStringRef string, MCStringRef other,
22592259

22602260
// Returns true if the string begins with the prefix string, processing as
22612261
// appropriate according to options.
2262-
MC_DLLEXPORT bool MCStringBeginsWith(MCStringRef string, MCStringRef prefix, MCStringOptions options);
2263-
MC_DLLEXPORT bool MCStringBeginsWithR(MCStringRef string, MCStringRef prefix, MCStringOptions options, uindex_t& r_self_match_length);
2264-
MC_DLLEXPORT bool MCStringSharedPrefix(MCStringRef self, MCRange p_range, MCStringRef p_prefix, MCStringOptions p_options, uindex_t& r_self_match_length);
2262+
// If 'r_string_match_length' is used, then it will contain the length of the
2263+
// match in 'string'. This might not be the same as the length of 'prefix' due
2264+
// to case folding and normalization concerns in unicode strings.
2265+
MC_DLLEXPORT bool MCStringBeginsWith(MCStringRef string, MCStringRef prefix, MCStringOptions options, uindex_t *r_string_match_length = nil);
2266+
MC_DLLEXPORT bool MCStringSharedPrefix(MCStringRef self, MCRange p_range, MCStringRef p_prefix, MCStringOptions p_options, uindex_t& r_string_match_length);
22652267
MC_DLLEXPORT bool MCStringBeginsWithCString(MCStringRef string, const char_t *prefix_cstring, MCStringOptions options);
22662268

22672269
// Returns true if the string ends with the suffix string, processing as
22682270
// appropriate according to options.
2269-
MC_DLLEXPORT bool MCStringEndsWith(MCStringRef string, MCStringRef suffix, MCStringOptions options);
2270-
MC_DLLEXPORT bool MCStringEndsWithR(MCStringRef string, MCStringRef suffix, MCStringOptions options, uindex_t& r_self_match_length);
2271-
MC_DLLEXPORT bool MCStringSharedSuffix(MCStringRef self, MCRange p_range, MCStringRef p_suffix, MCStringOptions p_options, uindex_t& r_self_match_length);
2271+
// If 'r_string_match_length' is used, then it will contain the length of the
2272+
// match in 'string'. This might not be the same as the length of 'suffix' due
2273+
// to case folding and normalization concerns in unicode strings.
2274+
MC_DLLEXPORT bool MCStringEndsWith(MCStringRef string, MCStringRef suffix, MCStringOptions options, uindex_t *r_string_match_length = nil);
2275+
MC_DLLEXPORT bool MCStringSharedSuffix(MCStringRef self, MCRange p_range, MCStringRef p_suffix, MCStringOptions p_options, uindex_t& r_string_match_length);
22722276
MC_DLLEXPORT bool MCStringEndsWithCString(MCStringRef string, const char_t *suffix_cstring, MCStringOptions options);
22732277

22742278
// Returns true if the string contains the given needle string, processing as

libfoundation/src/foundation-string.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,7 +2659,7 @@ compare_t MCStringCompareTo(MCStringRef self, MCStringRef p_other, MCStringOptio
26592659
}
26602660

26612661
MC_DLLEXPORT_DEF
2662-
bool MCStringBeginsWithR(MCStringRef self, MCStringRef p_prefix, MCStringOptions p_options, uindex_t& r_self_match_length)
2662+
bool MCStringBeginsWith(MCStringRef self, MCStringRef p_prefix, MCStringOptions p_options, uindex_t *r_self_match_length)
26632663
{
26642664
__MCAssertIsString(self);
26652665
__MCAssertIsString(p_prefix);
@@ -2681,7 +2681,8 @@ bool MCStringBeginsWithR(MCStringRef self, MCStringRef p_prefix, MCStringOptions
26812681
p_options))
26822682
return false;
26832683

2684-
r_self_match_length = p_prefix -> char_count;
2684+
if (r_self_match_length != nil)
2685+
*r_self_match_length = p_prefix -> char_count;
26852686
return true;
26862687
}
26872688

@@ -2692,13 +2693,6 @@ bool MCStringBeginsWithR(MCStringRef self, MCStringRef p_prefix, MCStringOptions
26922693
return MCUnicodeBeginsWith(self -> chars, self -> char_count, __MCStringIsNative(self), p_prefix -> chars, p_prefix -> char_count, __MCStringIsNative(p_prefix), (MCUnicodeCompareOption)p_options, r_self_match_length);
26932694
}
26942695

2695-
MC_DLLEXPORT_DEF
2696-
bool MCStringBeginsWith(MCStringRef self, MCStringRef p_prefix, MCStringOptions p_options)
2697-
{
2698-
uindex_t t_length;
2699-
return MCStringBeginsWithR(self, p_prefix, p_options, t_length);
2700-
}
2701-
27022696
MC_DLLEXPORT_DEF
27032697
bool MCStringSharedPrefix(MCStringRef self, MCRange p_range, MCStringRef p_prefix, MCStringOptions p_options, uindex_t& r_self_match_length)
27042698
{
@@ -2767,7 +2761,7 @@ bool MCStringBeginsWithCString(MCStringRef self, const char_t *p_prefix_cstring,
27672761
}
27682762

27692763
MC_DLLEXPORT_DEF
2770-
bool MCStringEndsWithR(MCStringRef self, MCStringRef p_suffix, MCStringOptions p_options, uindex_t& r_self_match_length)
2764+
bool MCStringEndsWith(MCStringRef self, MCStringRef p_suffix, MCStringOptions p_options, uindex_t *r_self_match_length)
27712765
{
27722766
__MCAssertIsString(self);
27732767
__MCAssertIsString(p_suffix);
@@ -2789,7 +2783,9 @@ bool MCStringEndsWithR(MCStringRef self, MCStringRef p_suffix, MCStringOptions p
27892783
p_options))
27902784
return false;
27912785

2792-
r_self_match_length = p_suffix -> char_count;
2786+
if (r_self_match_length != nil)
2787+
*r_self_match_length = p_suffix -> char_count;
2788+
27932789
return true;
27942790
}
27952791

@@ -2800,13 +2796,6 @@ bool MCStringEndsWithR(MCStringRef self, MCStringRef p_suffix, MCStringOptions p
28002796
return MCUnicodeEndsWith(self -> chars, self -> char_count, __MCStringIsNative(self), p_suffix -> chars, p_suffix -> char_count, __MCStringIsNative(p_suffix), (MCUnicodeCompareOption)p_options, r_self_match_length);
28012797
}
28022798

2803-
MC_DLLEXPORT_DEF
2804-
bool MCStringEndsWith(MCStringRef self, MCStringRef p_suffix, MCStringOptions p_options)
2805-
{
2806-
uindex_t t_length;
2807-
return MCStringEndsWithR(self, p_suffix, p_options, t_length);
2808-
}
2809-
28102799
MC_DLLEXPORT_DEF
28112800
bool MCStringSharedSuffix(MCStringRef self, MCRange p_range, MCStringRef p_suffix, MCStringOptions p_options, uindex_t& r_self_match_length)
28122801
{

libfoundation/src/foundation-unicode.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,27 +935,29 @@ int32_t MCUnicodeCompare(const void *p_first, uindex_t p_first_length, bool p_fi
935935

936936
bool MCUnicodeBeginsWith(const void *p_first, uindex_t p_first_length, bool p_first_native,
937937
const void *p_second, uindex_t p_second_length, bool p_second_native,
938-
MCUnicodeCompareOption p_option, uindex_t& r_first_match_length)
938+
MCUnicodeCompareOption p_option, uindex_t *r_first_match_length)
939939
{
940940
// Check for a shared prefix
941941
uindex_t t_first_match_length, t_second_match_length;
942942
MCUnicodeSharedPrefix(p_first, p_first_length, p_first_native, p_second, p_second_length, p_second_native, p_option, t_first_match_length, t_second_match_length);
943943
if (t_second_match_length != p_second_length)
944944
return false;
945-
r_first_match_length = t_first_match_length;
945+
if (r_first_match_length != nil)
946+
*r_first_match_length = t_first_match_length;
946947
return true;
947948
}
948949

949950
bool MCUnicodeEndsWith(const void *p_first, uindex_t p_first_length, bool p_first_native,
950951
const void *p_second, uindex_t p_second_length, bool p_second_native,
951-
MCUnicodeCompareOption p_option, uindex_t& r_first_match_length)
952+
MCUnicodeCompareOption p_option, uindex_t *r_first_match_length)
952953
{
953954
// Check for a shared suffix
954955
uindex_t t_first_match_length, t_second_match_length;
955956
MCUnicodeSharedSuffix(p_first, p_first_length, p_first_native, p_second, p_second_length, p_second_native, p_option, t_first_match_length, t_second_match_length);
956957
if (t_second_match_length != p_second_length)
957958
return false;
958-
r_first_match_length = t_first_match_length;
959+
if (r_first_match_length != nil)
960+
*r_first_match_length = t_first_match_length;
959961
return true;
960962
}
961963

0 commit comments

Comments
 (0)