Skip to content

Commit e096f29

Browse files
committed
Improved docs and added tests
1 parent ed92b3d commit e096f29

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

std/utf.d

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,7 +2459,8 @@ char[] toUTF8(return out char[4] buf, dchar c) nothrow @nogc @safe pure
24592459
}
24602460

24612461
/**
2462-
* Encodes string `s` into UTF-8 and returns the encoded string.
2462+
* Encodes the elements of `s` to UTF-8 and returns a newly allocated
2463+
* string of the elements.
24632464
*
24642465
* Params:
24652466
* s = the string to encode
@@ -2482,7 +2483,7 @@ string toUTF8(S)(S s) if (isInputRange!S && isSomeChar!(ElementEncodingType!S))
24822483
static if (hasLength!S || isSomeString!S)
24832484
app.reserve(s.length);
24842485

2485-
foreach (c; s.byUTF2!char)
2486+
foreach (c; s.byUTF!char)
24862487
app.put(c);
24872488

24882489
return app.data;
@@ -2501,6 +2502,18 @@ string toUTF8(S)(S s) if (isInputRange!S && isSomeChar!(ElementEncodingType!S))
25012502
assert("𐐷"d.toUTF8.equal([0xF0, 0x90, 0x90, 0xB7]));
25022503
}
25032504

2505+
@system pure unittest
2506+
{
2507+
import std.internal.test.dummyrange : ReferenceInputRange;
2508+
import std.algorithm.comparison : equal;
2509+
2510+
auto r1 = new ReferenceInputRange!dchar("Hellø");
2511+
auto r2 = new ReferenceInputRange!dchar("𐐷");
2512+
2513+
assert(r1.toUTF8.equal(['H', 'e', 'l', 'l', 0xC3, 0xB8]));
2514+
assert(r2.toUTF8.equal([0xF0, 0x90, 0x90, 0xB7]));
2515+
}
2516+
25042517

25052518
/* =================== Conversion to UTF16 ======================= */
25062519

0 commit comments

Comments
 (0)