From 4ccd938a43123558120f35994e9b96d65b6e299c Mon Sep 17 00:00:00 2001 From: kiith-sa Date: Fri, 2 Nov 2012 14:31:50 +0100 Subject: [PATCH] Utility function to create a loader from string (avoids API-break in future). --- dyaml/loader.d | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/dyaml/loader.d b/dyaml/loader.d index 87b823a..3a6ce8f 100644 --- a/dyaml/loader.d +++ b/dyaml/loader.d @@ -64,7 +64,7 @@ import dyaml.token; * "green: '#00ff00'\n" * "blue: '#0000ff'"; * - * auto colors = Loader(new MemoryStream(cast(char[])yaml_input)).load(); + * auto colors = Loader.fromString(yaml_input).load(); * * foreach(string color, string value; colors) * { @@ -125,6 +125,20 @@ struct Loader " for YAML loading: " ~ e.msg); } } + + /// Construct a Loader to load YAML from a string. + /// + /// Params: data = String to load YAML from. + /// + /// Returns: Loader loading YAML from given string. + static Loader fromString(string data) + { + return Loader(new MemoryStream(cast(char[])data)); + } + unittest + { + assert(Loader.fromString("42").load().as!int == 42); + } /** * Construct a Loader to load YAML from a _stream.