| Thread overview | |||||
|---|---|---|---|---|---|
|
July 14, 2017 nogc string concatenation? | ||||
|---|---|---|---|---|
| ||||
Anyone have an efficient implementation that is easy to use? | ||||
July 14, 2017 Re: nogc string concatenation? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to FoxyBrown | On Friday, 14 July 2017 at 00:40:38 UTC, FoxyBrown wrote: > Anyone have an efficient implementation that is easy to use? Not sure what you mean by efficient here, but a \theta(n+m) one is done idiomatically with Allocator+ranges like this (note that the casts to and from ubyte are necessary, because std.range.primitives.hasLength returns false for narrow strings, so we have to explicitly state that we want to work in code units): --- char[] strcat(Allocator)(Allocator alloc, string a, string b) { import std.experimental.allocator : makeArray; import std.range : chain; return cast(char[]) alloc.makeArray!ubyte(chain(cast(ubyte[]) a, cast(ubyte[]) b)); } --- Usage example: --- void main(string[] args) { import std.stdio; import std.exception : enforce; enforce(args.length == 3); import std.experimental.allocator : dispose; import std.experimental.allocator.mallocator; alias alloc = Mallocator.instance; char[] concated = alloc.strcat(args[1], args[2]); scope (exit) alloc.dispose(concated); writeln(cast(string) concated); } --- | |||
July 14, 2017 Re: nogc string concatenation? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to FoxyBrown | On Friday, 14 July 2017 at 00:40:38 UTC, FoxyBrown wrote:
> Anyone have an efficient implementation that is easy to use?
If you are OK with just a range spanning the two or more strings, then you could use chain as is.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply