Skip to content

Commit 03d32c6

Browse files
committed
Added Find Sequence to Spans
Added FindSequence to Span.h Refactored String find to use Span::Find_Seq in Ustring.cpp
1 parent 6efa557 commit 03d32c6

File tree

4 files changed

+120
-166
lines changed

4 files changed

+120
-166
lines changed

core/string/ustring.cpp

Lines changed: 75 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,69 +3041,42 @@ String String::substr(int p_from, int p_chars) const {
30413041
}
30423042

30433043
int String::find(const String &p_str, int p_from) const {
3044-
if (p_from < 0) {
3045-
return -1;
3046-
}
3047-
3048-
const int src_len = p_str.length();
3049-
3044+
const int str_len = p_str.length();
30503045
const int len = length();
30513046

3052-
if (src_len == 0 || len == 0) {
3053-
return -1; // won't find anything!
3047+
if (p_from < 0) {
3048+
p_from = len - str_len + p_from + 1;
30543049
}
3055-
3056-
if (src_len == 1) {
3057-
return find_char(p_str[0], p_from); // Optimize with single-char find.
3050+
if (p_from < 0 || p_from > len - str_len || p_str.is_empty()) {
3051+
return -1; // Still out of bounds
30583052
}
30593053

3060-
const char32_t *src = get_data();
3061-
const char32_t *str = p_str.get_data();
3062-
3063-
for (int i = p_from; i <= (len - src_len); i++) {
3064-
bool found = true;
3065-
for (int j = 0; j < src_len; j++) {
3066-
int read_pos = i + j;
3067-
3068-
if (read_pos >= len) {
3069-
ERR_PRINT("read_pos>=len");
3070-
return -1;
3071-
}
3072-
3073-
if (src[read_pos] != str[j]) {
3074-
found = false;
3075-
break;
3076-
}
3077-
}
3078-
3079-
if (found) {
3080-
return i;
3081-
}
3054+
if (p_str.length() == 1) {
3055+
// Optimize with single-char implementation.
3056+
return span().find(p_str[0], p_from);
30823057
}
30833058

3084-
return -1;
3059+
return span().find_sequence(p_str.span(), p_from);
30853060
}
30863061

30873062
int String::find(const char *p_str, int p_from) const {
3088-
if (p_from < 0 || !p_str) {
3089-
return -1;
3090-
}
3091-
3092-
const int src_len = strlen(p_str);
3093-
3063+
const int str_len = strlen(p_str);
30943064
const int len = length();
30953065

3096-
if (len == 0 || src_len == 0) {
3097-
return -1; // won't find anything!
3066+
if (p_from < 0) {
3067+
p_from = len - str_len + p_from + 1;
3068+
}
3069+
if (p_from < 0 || p_from > len - str_len || str_len == 0) {
3070+
return -1; // Still out of bounds
30983071
}
30993072

3100-
if (src_len == 1) {
3073+
if (str_len == 1) {
31013074
return find_char(*p_str, p_from); // Optimize with single-char find.
31023075
}
31033076

31043077
const char32_t *src = get_data();
31053078

3106-
if (src_len == 1) {
3079+
if (str_len == 1) {
31073080
const char32_t needle = p_str[0];
31083081

31093082
for (int i = p_from; i < len; i++) {
@@ -3113,13 +3086,13 @@ int String::find(const char *p_str, int p_from) const {
31133086
}
31143087

31153088
} else {
3116-
for (int i = p_from; i <= (len - src_len); i++) {
3089+
for (int i = p_from; i <= (len - str_len); i++) {
31173090
bool found = true;
3118-
for (int j = 0; j < src_len; j++) {
3091+
for (int j = 0; j < str_len; j++) {
31193092
int read_pos = i + j;
31203093

31213094
if (read_pos >= len) {
3122-
ERR_PRINT("read_pos>=len");
3095+
ERR_PRINT("read_pos>=length()");
31233096
return -1;
31243097
}
31253098

@@ -3156,7 +3129,7 @@ int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
31563129
return -1;
31573130
}
31583131

3159-
//int src_len=p_str.length();
3132+
//int str_len=p_str.length();
31603133
const String *keys = &p_keys[0];
31613134
int key_count = p_keys.size();
31623135
int len = length();
@@ -3204,24 +3177,24 @@ int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
32043177
}
32053178

32063179
int String::findn(const String &p_str, int p_from) const {
3180+
const int str_len = p_str.length();
3181+
const int len = length();
3182+
32073183
if (p_from < 0) {
3208-
return -1;
3184+
p_from = len - str_len + p_from + 1;
32093185
}
3210-
3211-
int src_len = p_str.length();
3212-
3213-
if (src_len == 0 || length() == 0) {
3214-
return -1; // won't find anything!
3186+
if (p_from < 0 || p_from > len - str_len || p_str.is_empty()) {
3187+
return -1; // Still out of bounds
32153188
}
32163189

32173190
const char32_t *srcd = get_data();
32183191

3219-
for (int i = p_from; i <= (length() - src_len); i++) {
3192+
for (int i = p_from; i <= (len - str_len); i++) {
32203193
bool found = true;
3221-
for (int j = 0; j < src_len; j++) {
3194+
for (int j = 0; j < str_len; j++) {
32223195
int read_pos = i + j;
32233196

3224-
if (read_pos >= length()) {
3197+
if (read_pos >= len) {
32253198
ERR_PRINT("read_pos>=length()");
32263199
return -1;
32273200
}
@@ -3244,24 +3217,24 @@ int String::findn(const String &p_str, int p_from) const {
32443217
}
32453218

32463219
int String::findn(const char *p_str, int p_from) const {
3220+
const int str_len = strlen(p_str);
3221+
const int len = length();
3222+
32473223
if (p_from < 0) {
3248-
return -1;
3224+
p_from = len - str_len + p_from + 1;
32493225
}
3250-
3251-
int src_len = strlen(p_str);
3252-
3253-
if (src_len == 0 || length() == 0) {
3254-
return -1; // won't find anything!
3226+
if (p_from < 0 || p_from > len - str_len || str_len == 0) {
3227+
return -1; // Still out of bounds
32553228
}
32563229

32573230
const char32_t *srcd = get_data();
32583231

3259-
for (int i = p_from; i <= (length() - src_len); i++) {
3232+
for (int i = p_from; i <= (len - str_len); i++) {
32603233
bool found = true;
3261-
for (int j = 0; j < src_len; j++) {
3234+
for (int j = 0; j < str_len; j++) {
32623235
int read_pos = i + j;
32633236

3264-
if (read_pos >= length()) {
3237+
if (read_pos >= len) {
32653238
ERR_PRINT("read_pos>=length()");
32663239
return -1;
32673240
}
@@ -3284,85 +3257,44 @@ int String::findn(const char *p_str, int p_from) const {
32843257
}
32853258

32863259
int String::rfind(const String &p_str, int p_from) const {
3287-
// establish a limit
3288-
int limit = length() - p_str.length();
3289-
if (limit < 0) {
3290-
return -1;
3291-
}
3260+
const int str_len = p_str.length();
3261+
const int len = length();
32923262

3293-
// establish a starting point
32943263
if (p_from < 0) {
3295-
p_from = limit;
3296-
} else if (p_from > limit) {
3297-
p_from = limit;
3264+
p_from = len - str_len + p_from + 1;
32983265
}
3299-
3300-
int src_len = p_str.length();
3301-
int len = length();
3302-
3303-
if (src_len == 0 || len == 0) {
3304-
return -1; // won't find anything!
3266+
if (p_from < 0 || p_from > len - str_len || p_str.is_empty()) {
3267+
return -1; // Still out of bounds
33053268
}
33063269

3307-
const char32_t *src = get_data();
3308-
3309-
for (int i = p_from; i >= 0; i--) {
3310-
bool found = true;
3311-
for (int j = 0; j < src_len; j++) {
3312-
int read_pos = i + j;
3313-
3314-
if (read_pos >= len) {
3315-
ERR_PRINT("read_pos>=len");
3316-
return -1;
3317-
}
3318-
3319-
if (src[read_pos] != p_str[j]) {
3320-
found = false;
3321-
break;
3322-
}
3323-
}
3324-
3325-
if (found) {
3326-
return i;
3327-
}
3270+
if (p_str.length() == 1) {
3271+
// Optimize with single-char implementation.
3272+
return span().rfind(p_str[0], p_from);
33283273
}
33293274

3330-
return -1;
3275+
return span().rfind_sequence(p_str.span(), p_from);
33313276
}
33323277

33333278
int String::rfind(const char *p_str, int p_from) const {
3334-
const int source_length = length();
3335-
int substring_length = strlen(p_str);
3336-
3337-
if (source_length == 0 || substring_length == 0) {
3338-
return -1; // won't find anything!
3339-
}
3340-
3341-
// establish a limit
3342-
int limit = length() - substring_length;
3343-
if (limit < 0) {
3344-
return -1;
3345-
}
3279+
const int str_len = strlen(p_str);
3280+
const int len = length();
33463281

3347-
// establish a starting point
3348-
int starting_point;
33493282
if (p_from < 0) {
3350-
starting_point = limit;
3351-
} else if (p_from > limit) {
3352-
starting_point = limit;
3353-
} else {
3354-
starting_point = p_from;
3283+
p_from = len - str_len + p_from + 1;
3284+
}
3285+
if (p_from < 0 || p_from > len - str_len || str_len == 0) {
3286+
return -1; // Still out of bounds
33553287
}
33563288

33573289
const char32_t *source = get_data();
33583290

3359-
for (int i = starting_point; i >= 0; i--) {
3291+
for (int i = p_from; i >= 0; i--) {
33603292
bool found = true;
3361-
for (int j = 0; j < substring_length; j++) {
3293+
for (int j = 0; j < str_len; j++) {
33623294
int read_pos = i + j;
33633295

3364-
if (read_pos >= source_length) {
3365-
ERR_PRINT("read_pos>=source_length");
3296+
if (read_pos >= length()) {
3297+
ERR_PRINT("read_pos>=length()");
33663298
return -1;
33673299
}
33683300

@@ -3392,35 +3324,25 @@ int String::rfind_char(char32_t p_char, int p_from) const {
33923324
}
33933325

33943326
int String::rfindn(const String &p_str, int p_from) const {
3395-
// establish a limit
3396-
int limit = length() - p_str.length();
3397-
if (limit < 0) {
3398-
return -1;
3399-
}
3327+
const int str_len = p_str.length();
3328+
const int len = length();
34003329

3401-
// establish a starting point
34023330
if (p_from < 0) {
3403-
p_from = limit;
3404-
} else if (p_from > limit) {
3405-
p_from = limit;
3331+
p_from = len - str_len + p_from + 1;
34063332
}
3407-
3408-
int src_len = p_str.length();
3409-
int len = length();
3410-
3411-
if (src_len == 0 || len == 0) {
3412-
return -1; // won't find anything!
3333+
if (p_from < 0 || p_from > len - str_len || p_str.is_empty()) {
3334+
return -1; // Still out of bounds
34133335
}
34143336

34153337
const char32_t *src = get_data();
34163338

34173339
for (int i = p_from; i >= 0; i--) {
34183340
bool found = true;
3419-
for (int j = 0; j < src_len; j++) {
3341+
for (int j = 0; j < str_len; j++) {
34203342
int read_pos = i + j;
34213343

34223344
if (read_pos >= len) {
3423-
ERR_PRINT("read_pos>=len");
3345+
ERR_PRINT("read_pos>=length()");
34243346
return -1;
34253347
}
34263348

@@ -3442,38 +3364,25 @@ int String::rfindn(const String &p_str, int p_from) const {
34423364
}
34433365

34443366
int String::rfindn(const char *p_str, int p_from) const {
3445-
const int source_length = length();
3446-
int substring_length = strlen(p_str);
3447-
3448-
if (source_length == 0 || substring_length == 0) {
3449-
return -1; // won't find anything!
3450-
}
3451-
3452-
// establish a limit
3453-
int limit = length() - substring_length;
3454-
if (limit < 0) {
3455-
return -1;
3456-
}
3367+
const int str_len = strlen(p_str);
3368+
const int len = length();
34573369

3458-
// establish a starting point
3459-
int starting_point;
34603370
if (p_from < 0) {
3461-
starting_point = limit;
3462-
} else if (p_from > limit) {
3463-
starting_point = limit;
3464-
} else {
3465-
starting_point = p_from;
3371+
p_from = len - str_len + p_from + 1;
3372+
}
3373+
if (p_from < 0 || p_from > len - str_len || str_len == 0) {
3374+
return -1; // Still out of bounds
34663375
}
34673376

34683377
const char32_t *source = get_data();
34693378

3470-
for (int i = starting_point; i >= 0; i--) {
3379+
for (int i = p_from; i >= 0; i--) {
34713380
bool found = true;
3472-
for (int j = 0; j < substring_length; j++) {
3381+
for (int j = 0; j < str_len; j++) {
34733382
int read_pos = i + j;
34743383

3475-
if (read_pos >= source_length) {
3476-
ERR_PRINT("read_pos>=source_length");
3384+
if (read_pos >= len) {
3385+
ERR_PRINT("read_pos>=length()");
34773386
return -1;
34783387
}
34793388

0 commit comments

Comments
 (0)