mirror of
https://github.com/godotengine/godot.git
synced 2025-01-30 21:33:18 +08:00
Merge pull request #147 from vinzenz/string-improvements
begins_with string version fix
This commit is contained in:
commit
219818075f
@ -479,6 +479,36 @@ bool test_26() {
|
|||||||
return captures.size();
|
return captures.size();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct test_27_data {
|
||||||
|
char const * data;
|
||||||
|
char const * begin;
|
||||||
|
bool expected;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool test_27() {
|
||||||
|
|
||||||
|
OS::get_singleton()->print("\n\nTest 26: begins_with\n");
|
||||||
|
test_27_data tc[] = {
|
||||||
|
{"res://foobar", "res://", true},
|
||||||
|
{"res", "res://", false},
|
||||||
|
{"abc", "abc", true}
|
||||||
|
};
|
||||||
|
size_t count = sizeof(tc) / sizeof(tc[0]);
|
||||||
|
bool state = true;
|
||||||
|
for (size_t i = 0;state && i < count; ++i) {
|
||||||
|
String s = tc[i].data;
|
||||||
|
state = s.begins_with(tc[i].begin) == tc[i].expected;
|
||||||
|
if (state) {
|
||||||
|
String sb = tc[i].begin;
|
||||||
|
state = s.begins_with(sb) == tc[i].expected;
|
||||||
|
}
|
||||||
|
if (!state) {
|
||||||
|
OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: ", tc[i].data, "\n\t\tbegin: ", tc[i].begin, "\n\t\texpected: ", tc[i].expected ? "true" : "false", "\n");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return state;
|
||||||
|
};
|
||||||
|
|
||||||
typedef bool (*TestFunc)(void);
|
typedef bool (*TestFunc)(void);
|
||||||
|
|
||||||
TestFunc test_funcs[] = {
|
TestFunc test_funcs[] = {
|
||||||
@ -509,6 +539,7 @@ TestFunc test_funcs[] = {
|
|||||||
test_24,
|
test_24,
|
||||||
test_25,
|
test_25,
|
||||||
test_26,
|
test_26,
|
||||||
|
test_27,
|
||||||
0
|
0
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -2491,19 +2491,21 @@ bool String::begins_with(const String& p_string) const {
|
|||||||
const CharType *src=&p_string[0];
|
const CharType *src=&p_string[0];
|
||||||
const CharType *str=&operator[](0);
|
const CharType *str=&operator[](0);
|
||||||
|
|
||||||
for (int i=0;i<l;i++) {
|
int i = 0;
|
||||||
|
for (;i<l;i++) {
|
||||||
|
|
||||||
if (src[i]!=str[i])
|
if (src[i]!=str[i])
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// only if i == l the p_string matches the beginning
|
||||||
|
return i == l;
|
||||||
|
|
||||||
}
|
}
|
||||||
bool String::begins_with(const char* p_string) const {
|
bool String::begins_with(const char* p_string) const {
|
||||||
|
|
||||||
int l=length();
|
int l=length();
|
||||||
if (l==0)
|
if (l==0||!p_string)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const CharType *str=&operator[](0);
|
const CharType *str=&operator[](0);
|
||||||
|
Loading…
Reference in New Issue
Block a user