splitWithQuotes

Splits a string into an array of strings by whitespace, but honours quotes.

Intended to be used with ASCII strings; may or may not work with more elaborate UTF-8 strings.

@safe
splitWithQuotes
(
string separator = " "
)
(
const string line
)

Parameters

separator

Separator string. May be more than one character.

line string

Input string.

Return Value

Type: auto

A string[] composed of the input string split up into substrings, delimited by whitespace. Quoted sections are treated as one substring.

Examples

string s = `title "this is my title" author "john doe"`;
immutable splitUp = splitWithQuotes(s);
assert(splitUp == [ "title", "this is my title", "author", "john doe" ]);
1 import std.conv : text;
2 
3 {
4     enum input = `title "this is my title" author "john doe"`;
5     immutable splitUp = splitWithQuotes(input);
6     immutable expected =
7     [
8         "title",
9         "this is my title",
10         "author",
11         "john doe"
12     ];
13     assert(splitUp == expected, splitUp.text);
14 }
15 {
16     enum input = `string without quotes`;
17     immutable splitUp = splitWithQuotes(input);
18     immutable expected =
19     [
20         "string",
21         "without",
22         "quotes",
23     ];
24     assert(splitUp == expected, splitUp.text);
25 }
26 {
27     enum input = string.init;
28     immutable splitUp = splitWithQuotes(input);
29     immutable expected = (string[]).init;
30     assert(splitUp == expected, splitUp.text);
31 }
32 {
33     enum input = `title "this is \"my\" title" author "john\\" doe`;
34     immutable splitUp = splitWithQuotes(input);
35     immutable expected =
36     [
37         "title",
38         `this is "my" title`,
39         "author",
40         `john\`,
41         "doe"
42     ];
43     assert(splitUp == expected, splitUp.text);
44 }
45 {
46     enum input = `title "this is \"my\" title" author "john\\\" doe`;
47     immutable splitUp = splitWithQuotes(input);
48     immutable expected =
49     [
50         "title",
51         `this is "my" title`,
52         "author",
53         `john\" doe`
54     ];
55     assert(splitUp == expected, splitUp.text);
56 }
57 {
58     enum input = `this has "unbalanced quotes`;
59     immutable splitUp = splitWithQuotes(input);
60     immutable expected =
61     [
62         "this",
63         "has",
64         "unbalanced quotes"
65     ];
66     assert(splitUp == expected, splitUp.text);
67 }
68 {
69     enum input = `""`;
70     immutable splitUp = splitWithQuotes(input);
71     immutable expected = (string[]).init;
72     assert(splitUp == expected, splitUp.text);
73 }
74 {
75     enum input = `"`;
76     immutable splitUp = splitWithQuotes(input);
77     immutable expected = (string[]).init;
78     assert(splitUp == expected, splitUp.text);
79 }
80 {
81     enum input = `"""""""""""`;
82     immutable splitUp = splitWithQuotes(input);
83     immutable expected = (string[]).init;
84     assert(splitUp == expected, splitUp.text);
85 }