From e54d84f4b799dd8d8ba0e156960451dcde6bc484 Mon Sep 17 00:00:00 2001 From: jacopodl Date: Thu, 18 Jan 2024 16:35:04 +0100 Subject: [PATCH] feat!: make some parameters optional in the replace, split, splitlines, and splitws methods for Bytes and String objects --- argon/vm/datatype/arstring.cpp | 66 +++++++++++++++++++++----------- argon/vm/datatype/bytes.cpp | 69 +++++++++++++++++++++++++--------- argon/vm/io/io.cpp | 2 +- 3 files changed, 97 insertions(+), 40 deletions(-) diff --git a/argon/vm/datatype/arstring.cpp b/argon/vm/datatype/arstring.cpp index de96567f..a4e18366 100644 --- a/argon/vm/datatype/arstring.cpp +++ b/argon/vm/datatype/arstring.cpp @@ -194,7 +194,7 @@ ARGON_METHOD(str_expandtabs, expandtabs, "Returns a copy of the string where all tab characters were replaced by spaces.\n" "\n" "- KWParameters:\n" - " - tabsize: Size of the tab; default step is 4.\n" + " - tabsize: Size of the tab; default step is 4.\n" "- Returns: A copy of the string where all tab characters were replaced by spaces.\n", nullptr, false, true) { IntegerUnderlying tabsize; @@ -302,14 +302,20 @@ ARGON_METHOD(str_replace, replace, "Returns a string where a specified value is replaced with a specified value.\n" "\n" "- Parameters:\n" - " - old: String to search for.\n" - " - new: String to replace the old value with.\n" - " - count: Number specifying how many occurrences of the old value you want to replace.\n" - " To replace all occurrence use -1.\n" + " - old: String to search for.\n" + " - new: String to replace the old value with.\n" + "- KWParameters:\n" + " - count: Number specifying how many occurrences of the old value you want to replace. " + "To replace all occurrence use -1.\n" "- Returns: String where a specified value is replaced.\n", - "s: old, s: new, i: count", false, false) { + "s: old, s: new", false, true) { + IntegerUnderlying count; + + if (!KParamLookupInt((Dict *) kwargs, "count", &count, -1)) + return nullptr; + return (ArObject *) StringReplace((String *) _self, (String *) args[0], - (String *) args[1], ((Integer *) args[2])->sint); + (String *) args[1], count); } ARGON_METHOD(str_reverse, reverse, @@ -422,13 +428,16 @@ ARGON_METHOD(str_split, split, "Splits the string at the specified separator and returns a list.\n" "\n" "- Parameters:\n" - " - pattern: Specifies the separator to use when splitting the string.\n" - " - maxsplit: Specifies how many splits to do.\n" + " - pattern: Specifies the separator to use when splitting the string.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of string.\n", - "sn: pattern, i: maxsplit", false, false) { + "sn: pattern", false, true) { const unsigned char *pattern = nullptr; ArSize plen = 0; + IntegerUnderlying maxsplit; + if (!IsNull(args[0])) { pattern = STR_BUF((String *) args[0]); plen = STR_LEN((String *) args[0]); @@ -439,35 +448,50 @@ ARGON_METHOD(str_split, split, } } + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) + return nullptr; + return support::Split(STR_BUF((String *) _self), pattern, (support::SplitChunkNewFn) StringNew, STR_LEN((String *) _self), plen, - ((Integer *) args[1])->sint); + maxsplit); } ARGON_METHOD(str_splitlines, splitlines, "Splits the string at the new line and returns a list.\n" "\n" - "- Parameters: maxsplit: Specifies how many splits to do.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of string.\n", - "i: maxsplit", false, false) { - return StringSplitLines((String *) _self, ((Integer *) args[0])->sint); + nullptr, false, true) { + IntegerUnderlying maxsplit; + + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) + return nullptr; + + return StringSplitLines((String *) _self, maxsplit); } ARGON_METHOD(str_splitws, splitws, "Splits the string at the whitespace and returns a list.\n" "\n" - "- Parameters: maxsplit: Specifies how many splits to do.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of string.\n", - "i: maxsplit", false, false) { + nullptr, false, true) { + IntegerUnderlying maxsplit; + + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) + return nullptr; + return support::Split(STR_BUF((String *) _self), nullptr, (support::SplitChunkNewFn) StringNew, STR_LEN((String *) _self), 0, - ((Integer *) args[0])->sint); + maxsplit); } ARGON_METHOD(str_startswith, startswith, @@ -1041,9 +1065,9 @@ String *argon::vm::datatype::StringFormat(const char *format, ...) { va_list args; String *str; - va_start(args, format); + va_start(args, format); str = StringFormat(format, args); - va_end(args); + va_end(args); return str; } @@ -1055,7 +1079,7 @@ String *argon::vm::datatype::StringFormat(const char *format, va_list args) { va_copy(vargs2, args); sz = vsnprintf(nullptr, 0, format, vargs2) + 1; // +1 is for '\0' - va_end(vargs2); + va_end(vargs2); if ((str = StringInit(sz - 1, true)) == nullptr) return nullptr; @@ -1064,7 +1088,7 @@ String *argon::vm::datatype::StringFormat(const char *format, va_list args) { va_copy(vargs2, args); vsnprintf((char *) STR_BUF(str), sz, format, vargs2); - va_end(vargs2); + va_end(vargs2); return str; } diff --git a/argon/vm/datatype/bytes.cpp b/argon/vm/datatype/bytes.cpp index f3b57a5d..1bbc7e2d 100644 --- a/argon/vm/datatype/bytes.cpp +++ b/argon/vm/datatype/bytes.cpp @@ -529,14 +529,20 @@ ARGON_METHOD(bytes_replace, replace, "Returns new bytes string where a specified value is replaced with a specified value.\n" "\n" "- Parameters:\n" - " - old: Bytes string to search for.\n" - " - new: Bytes string to replace the old value with.\n" - " - count: Number specifying how many occurrences of the old value you want to replace.\n" - " To replace all occurrence use -1.\n" + " - old: Bytes string to search for.\n" + " - new: Bytes string to replace the old value with.\n" + "- KWParameters:\n" + " - count: Number specifying how many occurrences of the old value you want to replace. " + "To replace all occurrence use -1.\n" "- Returns: Bytes string where a specified value is replaced.\n", - "x: old, x: new, i: count", false, false) { + "x: old, x: new", false, true) { + IntegerUnderlying count; + + if (!KParamLookupInt((Dict *) kwargs, "count", &count, -1)) + return nullptr; + return (ArObject *) BytesReplace((Bytes *) _self, (Bytes *) args[0], - (Bytes *) args[1], ((Integer *) args[2])->sint); + (Bytes *) args[1], count); } ARGON_METHOD(bytes_reverse, reverse, @@ -678,15 +684,17 @@ ARGON_METHOD(bytes_split, split, "Splits the bytes string at the specified separator and returns a list.\n" "\n" "- Parameters:\n" - " - pattern: Specifies the separator to use when splitting the bytes string.\n" - " - maxsplit: Specifies how many splits to do.\n" + " - pattern: Specifies the separator to use when splitting the bytes string.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of bytes string.\n", - ": pattern, i: maxsplit", false, false) { + ": pattern", false, true) { ArBuffer buffer{}; const unsigned char *pattern = nullptr; ArObject *ret; ArSize plen = 0; + IntegerUnderlying maxsplit; if (_self == args[0]) { ErrorFormat(kValueError[0], "cannot use the object to be split as a pattern"); @@ -701,15 +709,27 @@ ARGON_METHOD(bytes_split, split, plen = buffer.length; if (plen == 0) { + BufferRelease(&buffer); + ErrorFormat(kValueError[0], "empty separator"); return nullptr; } } + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) { + BufferRelease(&buffer); + + return nullptr; + } + std::shared_lock _(*((Bytes *) _self)); - ret = support::Split(BUFFER_GET((Bytes *) _self), pattern, (support::SplitChunkNewFn) BytesNew, - BUFFER_LEN((Bytes *) _self), plen, ((Integer *) args[1])->sint); + ret = support::Split(BUFFER_GET((Bytes *) _self), + pattern, + (support::SplitChunkNewFn) BytesNew, + BUFFER_LEN((Bytes *) _self), + plen, + maxsplit); BufferRelease(&buffer); @@ -719,24 +739,36 @@ ARGON_METHOD(bytes_split, split, ARGON_METHOD(bytes_splitlines, splitlines, "Splits the bytes string at the new line and returns a list.\n" "\n" - "- Parameters: maxsplit: Specifies how many splits to do.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of bytes string.\n", - "i: maxsplit", false, false) { + nullptr, false, true) { + IntegerUnderlying maxsplit; + + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) + return nullptr; + std::shared_lock _(*((Bytes *) _self)); return support::SplitLines( BUFFER_GET((Bytes *) _self), (support::SplitChunkNewFn) BytesNew, BUFFER_LEN((Bytes *) _self), - ((Integer *) args[0])->sint); + maxsplit); } ARGON_METHOD(bytes_splitws, splitws, "Splits the bytes string at the whitespace and returns a list.\n" "\n" - "- Parameters: maxsplit: Specifies how many splits to do.\n" + "- KWParameters:\n" + " - splits: Specifies how many splits to do.\n" "- Returns: New list of bytes string.\n", - "i: maxsplit", false, false) { + nullptr, false, true) { + IntegerUnderlying maxsplit; + + if (!KParamLookupInt((Dict *) kwargs, "splits", &maxsplit, -1)) + return nullptr; + std::shared_lock _(*((Bytes *) _self)); return support::Split(BUFFER_GET((Bytes *) _self), @@ -744,7 +776,7 @@ ARGON_METHOD(bytes_splitws, splitws, (support::SplitChunkNewFn) BytesNew, BUFFER_LEN((Bytes *) _self), 0, - ((Integer *) args[0])->sint); + maxsplit); } ARGON_METHOD(bytes_startswith, startswith, @@ -1080,7 +1112,8 @@ bool bytes_get_buffer(Bytes *self, ArBuffer *buffer, BufferFlags flags) { shared ? self->lock_shared() : self->lock(); - ok = BufferSimpleFill((ArObject *) self, buffer, flags, BUFFER_GET(self), 1, BUFFER_LEN(self), !BUFFER_FROZEN(self)); + ok = BufferSimpleFill((ArObject *) self, buffer, flags, BUFFER_GET(self), 1, BUFFER_LEN(self), + !BUFFER_FROZEN(self)); if (!ok) shared ? self->unlock_shared() : self->unlock(); diff --git a/argon/vm/io/io.cpp b/argon/vm/io/io.cpp index 1b6b7ae4..296a0086 100644 --- a/argon/vm/io/io.cpp +++ b/argon/vm/io/io.cpp @@ -12,7 +12,7 @@ const FunctionDef line_reader_methods[] = { ARGON_METHOD_STUB("readline", "Read line from the stream and return them.\n" "\n" - "As a convenience, if size is -1, all bytes until EOL are returned.\n" + "As a convenience, if size is -1, all bytes until newline or EOL are returned.\n" "With size = -1, readline() may be using multiple calls to the stream.\n" "\n" "- Parameter size: Number of bytes to read from the stream or -1 to read entire line.\n"