Skip to content

Commit

Permalink
feat!: make some parameters optional in the replace, split, splitline…
Browse files Browse the repository at this point in the history
…s, and splitws methods for Bytes and String objects
  • Loading branch information
jacopodl committed Jan 18, 2024
1 parent abfbaae commit e54d84f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 40 deletions.
66 changes: 45 additions & 21 deletions argon/vm/datatype/arstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]);
Expand All @@ -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<String>) 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<String>) StringNew,
STR_LEN((String *) _self),
0,
((Integer *) args[0])->sint);
maxsplit);
}

ARGON_METHOD(str_startswith, startswith,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
69 changes: 51 additions & 18 deletions argon/vm/datatype/bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand All @@ -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<Bytes>) BytesNew,
BUFFER_LEN((Bytes *) _self), plen, ((Integer *) args[1])->sint);
ret = support::Split(BUFFER_GET((Bytes *) _self),
pattern,
(support::SplitChunkNewFn<Bytes>) BytesNew,
BUFFER_LEN((Bytes *) _self),
plen,
maxsplit);

BufferRelease(&buffer);

Expand All @@ -719,32 +739,44 @@ 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<Bytes>) 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),
nullptr,
(support::SplitChunkNewFn<Bytes>) BytesNew,
BUFFER_LEN((Bytes *) _self),
0,
((Integer *) args[0])->sint);
maxsplit);
}

ARGON_METHOD(bytes_startswith, startswith,
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion argon/vm/io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e54d84f

Please sign in to comment.