Skip to content

Commit 44624f9

Browse files
committed
remove support for UTF-16/32
1 parent 3a5a2c5 commit 44624f9

File tree

17 files changed

+45
-373
lines changed

17 files changed

+45
-373
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ To start using it in your project, see the [Getting Started](https://dlang-commu
2020
- Supports all YAML 1.1 constructs. All examples from the YAML 1.1
2121
specification are parsed correctly.
2222
- Reads from and writes from/to YAML files or in-memory buffers.
23-
- UTF-8, UTF-16 and UTF-32 encodings are supported, both big and
24-
little endian (plain ASCII also works as it is a subset of UTF-8).
23+
- UTF-8 is the only supported encoding.
2524
- Support for both block (Python-like, based on indentation) and flow
2625
(JSON-like, based on bracing) constructs.
2726
- Support for YAML anchors and aliases.

dub.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@
66
"Cameron \"Herringway\" Ross"
77
],
88
"license": "BSL-1.0",
9-
"dependencies": {
10-
"tinyendian" : "~>0.2.0"
11-
},
129
"homepage": "https://github.com/dlang-community/D-YAML",
1310
"copyright": "Copyright © 2011-2018, Ferdinand Majerech",
1411
"configurations": [
1512
{ "name": "library" },
1613
{ "name": "unittest" },
1714
{
1815
"name": "unittest-dip1000",
19-
"dflags": [ "-preview=dip1000" ],
20-
"dependencies": {
21-
"tinyendian": { "version": "*", "dflags" : [ "-preview=dip1000" ] },
22-
}
16+
"dflags": [ "-preview=dip1000" ]
2317
}
2418
],
2519
"subPackages": [

examples/tokens/source/app.d

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import std.uni;
2222

2323
void dumpEventString(string str) @safe
2424
{
25-
auto events = new Parser(Scanner(Reader(cast(ubyte[])str.dup)));
25+
auto events = new Parser(Scanner(Reader(str.dup)));
2626
foreach (event; events)
2727
{
2828
writeln(event);
2929
}
3030
}
3131
void dumpTokens(string str) @safe
3232
{
33-
writefln("%(%s\n%)", new Parser(Scanner(Reader(cast(ubyte[])str.dup))));
33+
writefln("%(%s\n%)", new Parser(Scanner(Reader(str.dup))));
3434
}
3535

3636

examples/yaml_bench/yaml_bench.d

+7-7
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ void main(string[] args) //@safe
9494
string file = args[1];
9595

9696
auto stopWatch = StopWatch(AutoStart.yes);
97-
void[] fileInMemory;
98-
if(!reload) { fileInMemory = std.file.read(file); }
99-
void[] fileWorkingCopy = fileInMemory.dup;
97+
char[] fileInMemory;
98+
if(!reload) { fileInMemory = cast(char[])std.file.read(file); }
99+
char[] fileWorkingCopy = fileInMemory.dup;
100100
auto loadTime = stopWatch.peek();
101101
stopWatch.reset();
102102
try
@@ -113,13 +113,13 @@ void main(string[] args) //@safe
113113
{
114114
// Loading the file rewrites the loaded buffer, so if we don't reload from
115115
// disk, we need to use a copy of the originally loaded file.
116-
if(reload) { fileInMemory = std.file.read(file); }
116+
if(reload) { fileInMemory = cast(char[])std.file.read(file); }
117117
else { fileWorkingCopy[] = fileInMemory[]; }
118-
void[] fileToLoad = reload ? fileInMemory : fileWorkingCopy;
118+
char[] fileToLoad = reload ? fileInMemory : fileWorkingCopy;
119119

120120
if(scanOnly)
121121
{
122-
auto reader = Reader(cast(ubyte[])fileToLoad, "benchmark");
122+
auto reader = Reader(fileToLoad, "benchmark");
123123
auto scanner = Scanner(reader);
124124
while(!scanner.empty)
125125
{
@@ -128,7 +128,7 @@ void main(string[] args) //@safe
128128
}
129129
else
130130
{
131-
auto loader = Loader.fromBuffer(fileToLoad);
131+
auto loader = Loader.fromString(fileToLoad);
132132
loader.resolver = resolver;
133133
nodes = loader.array;
134134
}

examples/yaml_gen/yaml_gen.d

+2-8
Original file line numberDiff line numberDiff line change
@@ -298,16 +298,10 @@ void main(string[] args)
298298
Node[] generated = generate(configFile);
299299

300300
auto dumper = dumper();
301-
auto encoding = config["encoding"];
302-
303301
dumper.indent = config["indent"].as!uint;
304302
dumper.textWidth = config["text-width"].as!uint;
305-
switch(encoding.as!string)
306-
{
307-
case "utf-16": dumper.dump!wchar(File(args[1], "w").lockingTextWriter, generated); break;
308-
case "utf-32": dumper.dump!dchar(File(args[1], "w").lockingTextWriter, generated); break;
309-
default: dumper.dump!char(File(args[1], "w").lockingTextWriter, generated); break;
310-
}
303+
304+
dumper.dump(File(args[1], "w").lockingTextWriter, generated);
311305
}
312306
catch(YAMLException e)
313307
{

meson.build

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ dyaml_src = [
3939
]
4040
install_subdir('source/dyaml', install_dir: 'include/d/yaml/')
4141

42-
tinyendian_dep = dependency('tinyendian', version: '>=0.2.0', fallback: ['tinyendian', 'tinyendian_dep'])
43-
4442
dyaml_lib = library('dyaml',
4543
[dyaml_src],
4644
include_directories: [src_dir],
47-
dependencies: [tinyendian_dep],
4845
install: true,
4946
version: meson.project_version(),
5047
soversion: project_soversion
@@ -59,6 +56,5 @@ pkgc.generate(name: 'dyaml',
5956
# Make D-YAML easy to use as subproject
6057
dyaml_dep = declare_dependency(
6158
link_with: dyaml_lib,
62-
include_directories: [src_dir],
63-
dependencies: [tinyendian_dep]
59+
include_directories: [src_dir]
6460
)

source/dyaml/dumper.d

+3-4
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,12 @@ struct Dumper
143143
* Throws: YAMLException on error (e.g. invalid nodes,
144144
* unable to write to file/stream).
145145
*/
146-
void dump(CharacterType = char, Range)(Range range, Node[] documents ...)
147-
if (isOutputRange!(Range, CharacterType) &&
148-
isOutputRange!(Range, char) || isOutputRange!(Range, wchar) || isOutputRange!(Range, dchar))
146+
void dump(Range)(Range range, Node[] documents ...)
147+
if (isOutputRange!(Range, char))
149148
{
150149
try
151150
{
152-
auto emitter = new Emitter!(Range, CharacterType)(range, canonical, indent_, textWidth, lineBreak);
151+
auto emitter = new Emitter!Range(range, canonical, indent_, textWidth, lineBreak);
153152
auto serializer = Serializer(resolver, explicitStart ? Yes.explicitStart : No.explicitStart,
154153
explicitEnd ? Yes.explicitEnd : No.explicitEnd, YAMLVersion, tags_);
155154
serializer.startStream(emitter);

source/dyaml/emitter.d

+10-30
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import std.algorithm;
1414
import std.array;
1515
import std.ascii;
1616
import std.conv;
17-
import std.encoding;
1817
import std.exception;
1918
import std.format;
2019
import std.range;
@@ -23,7 +22,6 @@ import std.system;
2322
import std.typecons;
2423
import std.utf;
2524

26-
import dyaml.encoding;
2725
import dyaml.escapes;
2826
import dyaml.event;
2927
import dyaml.exception;
@@ -67,7 +65,7 @@ private alias isFlowIndicator = among!(',', '?', '[', ']', '{', '}');
6765
private alias isSpace = among!('\0', '\n', '\r', '\u0085', '\u2028', '\u2029', ' ', '\t');
6866

6967
//Emits YAML events into a file/stream.
70-
struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
68+
struct Emitter(Range) if (isOutputRange!(Range, char))
7169
{
7270
private:
7371
///Default tag handle shortcuts and replacements.
@@ -222,20 +220,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
222220
///Write a string to the file/stream.
223221
void writeString(const scope char[] str) @safe
224222
{
225-
static if(is(CharType == char))
226-
{
227-
copy(str, stream_);
228-
}
229-
static if(is(CharType == wchar))
230-
{
231-
const buffer = to!wstring(str);
232-
copy(buffer, stream_);
233-
}
234-
static if(is(CharType == dchar))
235-
{
236-
const buffer = to!dstring(str);
237-
copy(buffer, stream_);
238-
}
223+
copy(str, stream_);
239224
}
240225

241226
///In some cases, we wait for a few next events before emitting.
@@ -731,7 +716,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
731716
//{
732717
// writeIndent();
733718
//}
734-
auto writer = ScalarWriter!(Range, CharType)(&this, analysis_.scalar,
719+
auto writer = ScalarWriter!Range(&this, analysis_.scalar,
735720
context_ != Context.mappingSimpleKey);
736721
final switch(style_)
737722
{
@@ -1172,14 +1157,9 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
11721157

11731158
//Writers.
11741159

1175-
///Start the YAML stream (write the unicode byte order mark).
1160+
///Start the YAML stream (do nothing).
11761161
void writeStreamStart() @safe
11771162
{
1178-
//Write BOM (except for UTF-8)
1179-
static if(is(CharType == wchar) || is(CharType == dchar))
1180-
{
1181-
stream_.put(cast(CharType)'\uFEFF');
1182-
}
11831163
}
11841164

11851165
///End the YAML stream.
@@ -1274,7 +1254,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
12741254
private:
12751255

12761256
///RAII struct used to write out scalar values.
1277-
struct ScalarWriter(Range, CharType)
1257+
struct ScalarWriter(Range)
12781258
{
12791259
invariant()
12801260
{
@@ -1283,14 +1263,14 @@ struct ScalarWriter(Range, CharType)
12831263
}
12841264

12851265
private:
1286-
@disable int opCmp(ref Emitter!(Range, CharType));
1287-
@disable bool opEquals(ref Emitter!(Range, CharType));
1266+
@disable int opCmp(ref Emitter!(Range));
1267+
@disable bool opEquals(ref Emitter!(Range));
12881268

12891269
///Used as "null" UTF-32 character.
12901270
static immutable dcharNone = dchar.max;
12911271

12921272
///Emitter used to emit the scalar.
1293-
Emitter!(Range, CharType)* emitter_;
1273+
Emitter!Range* emitter_;
12941274

12951275
///UTF-8 encoded text of the scalar to write.
12961276
string text_;
@@ -1311,7 +1291,7 @@ struct ScalarWriter(Range, CharType)
13111291

13121292
public:
13131293
///Construct a ScalarWriter using emitter to output text.
1314-
this(Emitter!(Range, CharType)* emitter, string text, const bool split = true) @safe nothrow
1294+
this(Emitter!Range* emitter, string text, const bool split = true) @safe nothrow
13151295
{
13161296
emitter_ = emitter;
13171297
text_ = text;
@@ -1506,7 +1486,7 @@ struct ScalarWriter(Range, CharType)
15061486
///Write text as plain scalar.
15071487
void writePlain() @safe
15081488
{
1509-
if(emitter_.context_ == Emitter!(Range, CharType).Context.root){emitter_.openEnded_ = true;}
1489+
if(emitter_.context_ == Emitter!Range.Context.root){emitter_.openEnded_ = true;}
15101490
if(text_ == ""){return;}
15111491
if(!emitter_.whitespace_)
15121492
{

source/dyaml/encoding.d

-11
This file was deleted.

source/dyaml/loader.d

+5-69
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct Loader
5757
{
5858
try
5959
{
60-
auto loader = Loader(std.file.read(filename), filename);
60+
auto loader = Loader(cast(char[])std.file.read(filename), filename);
6161
return loader;
6262
}
6363
catch(FileException e)
@@ -69,7 +69,7 @@ struct Loader
6969
/// ditto
7070
static Loader fromFile(File file) @system
7171
{
72-
auto loader = Loader(file.byChunk(4096).join, file.name);
72+
auto loader = Loader(cast(char[])file.byChunk(4096).join, file.name);
7373
return loader;
7474
}
7575

@@ -88,7 +88,7 @@ struct Loader
8888
*/
8989
static Loader fromString(char[] data, string filename = "<unknown>") @safe
9090
{
91-
return Loader(cast(ubyte[])data, filename);
91+
return Loader(data, filename);
9292
}
9393
/// Ditto
9494
static Loader fromString(string data, string filename = "<unknown>") @safe
@@ -105,40 +105,9 @@ struct Loader
105105
{
106106
assert(Loader.fromString("42").load().as!int == 42);
107107
}
108-
109-
/** Construct a Loader to load YAML from a buffer.
110-
*
111-
* Params: yamlData = Buffer with YAML data to load. This may be e.g. a file
112-
* loaded to memory or a string with YAML data. Note that
113-
* buffer $(B will) be overwritten, as D:YAML minimizes
114-
* memory allocations by reusing the input _buffer.
115-
* $(B Must not be deleted or modified by the user as long
116-
* as nodes loaded by this Loader are in use!) - Nodes may
117-
* refer to data in this buffer.
118-
*
119-
* Note that D:YAML looks for byte-order-marks YAML files encoded in
120-
* UTF-16/UTF-32 (and sometimes UTF-8) use to specify the encoding and
121-
* endianness, so it should be enough to load an entire file to a buffer and
122-
* pass it to D:YAML, regardless of Unicode encoding.
123-
*
124-
* Throws: YAMLException if yamlData contains data illegal in YAML.
125-
*/
126-
static Loader fromBuffer(ubyte[] yamlData) @safe
127-
{
128-
return Loader(yamlData);
129-
}
130-
/// Ditto
131-
static Loader fromBuffer(void[] yamlData) @system
132-
{
133-
return Loader(yamlData);
134-
}
135-
/// Ditto
136-
private this(void[] yamlData, string name = "<unknown>") @system
137-
{
138-
this(cast(ubyte[])yamlData, name);
139-
}
108+
deprecated("Use Loader.fromString instead") alias fromBuffer = fromString;
140109
/// Ditto
141-
private this(ubyte[] yamlData, string name = "<unknown>") @safe
110+
private this(char[] yamlData, string name = "<unknown>") @safe
142111
{
143112
try
144113
{
@@ -430,36 +399,3 @@ EOS";
430399
{
431400
assertThrown(Loader.fromFile("test/data/odd-utf16.stream-error").load());
432401
}
433-
434-
// UTF-16 and 32 test
435-
@safe unittest
436-
{
437-
import std.conv : to;
438-
import std.range : only;
439-
enum string yaml = `ABCDØ`;
440-
enum bom = '\uFEFF';
441-
foreach (doc; only(
442-
cast(ubyte[])(bom~yaml.to!(wchar[])),
443-
cast(ubyte[])(bom~yaml.to!(dchar[])),
444-
))
445-
{
446-
assert(Loader.fromBuffer(doc).load().as!string == yaml);
447-
}
448-
}
449-
// Invalid unicode test
450-
@safe unittest
451-
{
452-
import std.conv : to;
453-
import std.range : only;
454-
enum string yaml = `ABCDØ`;
455-
enum badBOM = '\uFFFE';
456-
foreach (doc; only(
457-
cast(ubyte[])yaml.to!(wchar[]),
458-
cast(ubyte[])yaml.to!(dchar[]),
459-
cast(ubyte[])(badBOM~yaml.to!(wchar[])),
460-
cast(ubyte[])(badBOM~yaml.to!(dchar[])),
461-
))
462-
{
463-
assertThrown(Loader.fromBuffer(doc).load());
464-
}
465-
}

source/dyaml/package.d

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module dyaml;
77

88
public import dyaml.dumper;
9-
public import dyaml.encoding;
109
public import dyaml.exception;
1110
public import dyaml.linebreak;
1211
public import dyaml.loader;

0 commit comments

Comments
 (0)