From 0425becba9c0d3742910ef32506702f58e1efe58 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Fri, 1 Dec 2023 22:19:31 +0900 Subject: [PATCH] Support start, end in str.startswith, str.endswith --- src/obj_str.c | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/obj_str.c b/src/obj_str.c index 5460738..0a68a49 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -825,17 +825,47 @@ KRK_Method(str,index) { } KRK_Method(str,startswith) { - METHOD_TAKES_EXACTLY(1); /* I know the Python versions of these take optional start, end... */ - CHECK_ARG(1,str,KrkString*,prefix); - return BOOLEAN_VAL(substringMatch(self->chars,self->length,prefix->chars,prefix->length)); + KrkString * substr; + int start = 0; + int end = self->codesLength; + if (!krk_parseArgs(".O!|ii",(const char*[]){"prefix","start","end"}, KRK_BASE_CLASS(str), &substr, &start, &end)) { + return NONE_VAL(); + } + + WRAP_INDEX(start); + WRAP_INDEX(end); + + krk_unicodeString(self); + krk_unicodeString(substr); + + if (end < start || (size_t)(end - start) < substr->codesLength) return BOOLEAN_VAL(0); + + for (size_t i = 0; i < substr->codesLength; ++i) { + if (KRK_STRING_FAST(self, start + i) != KRK_STRING_FAST(substr,i)) return BOOLEAN_VAL(0); + } + return BOOLEAN_VAL(1); } KRK_Method(str,endswith) { - METHOD_TAKES_EXACTLY(1); /* I know the Python versions of these take optional start, end... */ - CHECK_ARG(1,str,KrkString*,suffix); - if (suffix->length > self->length) return BOOLEAN_VAL(0); - return BOOLEAN_VAL(substringMatch(self->chars + (self->length - suffix->length), - suffix->length, suffix->chars, suffix->length)); + KrkString * substr; + int start = 0; + int end = self->codesLength; + if (!krk_parseArgs(".O!|ii",(const char*[]){"suffix","start","end"}, KRK_BASE_CLASS(str), &substr, &start, &end)) { + return NONE_VAL(); + } + + WRAP_INDEX(start); + WRAP_INDEX(end); + + krk_unicodeString(self); + krk_unicodeString(substr); + + if (end < start || (size_t)(end - start) < substr->codesLength) return BOOLEAN_VAL(0); + + for (size_t i = 0; i < substr->codesLength; ++i) { + if (KRK_STRING_FAST(self, (end - i - 1)) != KRK_STRING_FAST(substr,(substr->codesLength - i - 1))) return BOOLEAN_VAL(0); + } + return BOOLEAN_VAL(1); } /**