Skip to content

Commit

Permalink
feat[stdlib]: add concat method for StringBuilder (#217)
Browse files Browse the repository at this point in the history
It returns `self`, thus allowing chaining of `concat` calls
  • Loading branch information
Gusarich authored Apr 1, 2024
1 parent ea5191a commit a7f3ad0
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Reserve mode constants in `@stdlib/reserve`, namely `ReserveExact`, `ReserveAllExcept`, `ReserveAtMost`, `ReserveAddOriginalBalance`, `ReserveInvertSign`, `ReserveBounceIfActionFail`: PR [#173](https://github.com/tact-lang/tact/pull/173)
- JSON Schema for `tact.config.json`: PR [#194](https://github.com/tact-lang/tact/pull/194)
- Display an error for integer overflow at compile-time: PR [#200](https://github.com/tact-lang/tact/pull/200)
- Non-modifying `StringBuilder`'s `concat` method for chained string concatenations: PR [#217](https://github.com/tact-lang/tact/pull/217)

### Changed
- Update the `dump` function to handle addresses: PR [#175](https://github.com/tact-lang/tact/pull/175)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2996,6 +2996,21 @@ return ((builders), ());",
"name": "__tact_string_builder_append",
"signature": "((tuple), ()) __tact_string_builder_append(tuple builders, slice sc)",
},
{
"code": {
"code": "builders~__tact_string_builder_append(sc);
return builders;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_string_builder_append",
},
"flags": Set {},
"name": "__tact_string_builder_append_not_mut",
"signature": "(tuple) __tact_string_builder_append_not_mut(tuple builders, slice sc)",
},
{
"code": {
"code": "var b = begin_cell();
Expand Down Expand Up @@ -6978,6 +6993,21 @@ return ((builders), ());",
"name": "__tact_string_builder_append",
"signature": "((tuple), ()) __tact_string_builder_append(tuple builders, slice sc)",
},
{
"code": {
"code": "builders~__tact_string_builder_append(sc);
return builders;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_string_builder_append",
},
"flags": Set {},
"name": "__tact_string_builder_append_not_mut",
"signature": "(tuple) __tact_string_builder_append_not_mut(tuple builders, slice sc)",
},
{
"code": {
"code": "var b = begin_cell();
Expand Down Expand Up @@ -10960,6 +10990,21 @@ return ((builders), ());",
"name": "__tact_string_builder_append",
"signature": "((tuple), ()) __tact_string_builder_append(tuple builders, slice sc)",
},
{
"code": {
"code": "builders~__tact_string_builder_append(sc);
return builders;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_string_builder_append",
},
"flags": Set {},
"name": "__tact_string_builder_append_not_mut",
"signature": "(tuple) __tact_string_builder_append_not_mut(tuple builders, slice sc)",
},
{
"code": {
"code": "var b = begin_cell();
Expand Down
11 changes: 11 additions & 0 deletions src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,17 @@ export function writeStdlib(ctx: WriterContext) {
});
});

ctx.fun(`__tact_string_builder_append_not_mut`, () => {
ctx.signature(`(tuple) __tact_string_builder_append_not_mut(tuple builders, slice sc)`);
ctx.context('stdlib');
ctx.body(() => {
ctx.write(`
builders~${ctx.used('__tact_string_builder_append')}(sc);
return builders;
`);
});
});

ctx.fun(`__tact_int_to_string`, () => {
ctx.signature(`slice __tact_int_to_string(int src)`);
ctx.context('stdlib');
Expand Down
44 changes: 23 additions & 21 deletions src/imports/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,27 +228,29 @@ files['std/text.tact'] =
'KF9fdGFjdF9zdHJpbmdfYnVpbGRlcl9zdGFydF90YWlsX3N0cmluZykKbmF0aXZlIGJlZ2luVGFpbFN0cmluZygpOiBTdHJpbmdCdWlsZGVyOwoKQG5hbWUoX190YWN0' +
'X3N0cmluZ19idWlsZGVyX3N0YXJ0KQpuYXRpdmUgYmVnaW5TdHJpbmdGcm9tQnVpbGRlcihiOiBCdWlsZGVyKTogU3RyaW5nQnVpbGRlcjsKCkBuYW1lKF9fdGFj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ICAgICAgdGhyb3coMTM0KTsKICAgICAgICB9CiAgICB9CgogICAgLy8gUGFkZGluZwogICAgbGV0IHRvdGFsOiBJbnQgPSByZXN1bHQuYml0cygp' +
'OwogICAgbGV0IHBhZGRpbmc6IEludCA9IHRvdGFsICUgODsKICAgIGlmIChwYWRkaW5nICE9IDApIHsKICAgICAgICBsZXQgczogU2xpY2UgPSByZXN1bHQuYXNTbGlj' +
'ZSgpOwogICAgICAgIHJldHVybiBzLmxvYWRCaXRzKHRvdGFsIC0gcGFkZGluZyk7CiAgICB9IGVsc2UgewogICAgICAgIHJldHVybiByZXN1bHQuYXNTbGljZSgpOwog' +
'ICAgfQp9';
files['stdlib_ex.fc'] =
'Zm9yYWxsIFggLT4gdHVwbGUgX190YWN0X3NldCh0dXBsZSB4LCBYIHYsIGludCBpKSBhc20gIlNFVElOREVYVkFSUSI7CigpIF9fdGFjdF9ub3AoKSBhc20gIk5PUCI7' +
'CnNsaWNlIF9fdGFjdF9zdHJfdG9fc2xpY2Uoc2xpY2UgcykgYXNtICJOT1AiOwpzbGljZSBfX3RhY3Rfc2xpY2VfdG9fc3RyKHNsaWNlIHMpIGFzbSAiTk9QIjsKc2xp' +
Expand Down
1 change: 1 addition & 0 deletions src/test/feature-strings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('feature-strings', () => {
const l = 'привет мир 👀 привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀';
expect(await contract.getConstantStringUnicodeLong()).toBe(l);
expect((await contract.getDynamicStringCell()).equals(beginCell().storeStringTail('Hello!').endCell())).toBe(true);
expect((await contract.getDynamicStringCell2()).equals(beginCell().storeStringTail('Hello, World!').endCell())).toBe(true);
expect((await contract.getDynamicCommentCell()).equals(beginCell().storeUint(0, 32).storeStringTail('Something something world!').endCell())).toBe(true);
const l2 = 'Hello!привет мир 👀 привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀привет мир 👀';
expect((await contract.getDynamicCommentCellLarge()).equals(beginCell().storeStringTail(l2).endCell())).toBe(true);
Expand Down
6 changes: 6 additions & 0 deletions src/test/features/strings.tact
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ contract StringsTester {
return b.toCell();
}

get fun dynamicStringCell2(): Cell {
let b: StringBuilder = beginString();
b = b.concat("Hello,").concat(" ").concat("World!");
return b.toCell();
}

get fun dynamicCommentCell(): Cell {
let b: StringBuilder = beginComment();
b.append("Something something world!");
Expand Down
3 changes: 3 additions & 0 deletions stdlib/std/text.tact
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ native beginStringFromBuilder(b: Builder): StringBuilder;
@name(__tact_string_builder_append)
extends mutates native append(self: StringBuilder, s: String);

@name(__tact_string_builder_append_not_mut)
extends native concat(self: StringBuilder, s: String): StringBuilder;

@name(__tact_string_builder_end)
extends native toCell(self: StringBuilder): Cell;

Expand Down

0 comments on commit a7f3ad0

Please sign in to comment.