splitInto

Splits a string by a passed separator and assign the delimited words to the passed strings by ref. Overload that stores overflow strings into a passed array.

Note: *Does* take quoted substrings into consideration.

  1. auto splitInto(string slice, Strings strings)
  2. auto splitInto(string slice, Strings strings, string[] overflow)
    @safe
    splitInto
    (
    string separator = " "
    Strings...
    )
    (
    const string slice
    ,
    ref Strings strings
    ,
    out string[] overflow
    )
    if (
    !Strings.length ||
    (
    is(Strings[0] == string) &&
    allSameType!Strings
    )
    )

Parameters

separator

What token to separate the input string into words with.

slice string

Input string of words separated by separator.

strings Strings

Variadic list of strings to assign the split words in slice.

overflow string[]

Overflow array.

Return Value

Type: auto

A SplitResults with the results of the split attempt.

Examples

1 import lu.conv : Enum;
2 import std.conv : text;
3 
4 {
5     string line = "abc def ghi";
6     string abc, def, ghi;
7     string[] overflow;
8     immutable results = line.splitInto(abc, def, ghi, overflow);
9 
10     assert((abc == "abc"), abc);
11     assert((def == "def"), def);
12     assert((ghi == "ghi"), ghi);
13     assert(!overflow.length, overflow.text);
14     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
15 }
16 {
17     string line = "abc##def##ghi";
18     string abc, def, ghi;
19     string[] overflow;
20     immutable results = line.splitInto!"##"(abc, def, ghi, overflow);
21 
22     assert((abc == "abc"), abc);
23     assert((def == "def"), def);
24     assert((ghi == "ghi"), ghi);
25     assert(!overflow.length, overflow.text);
26     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
27 }
28 {
29     string line = "abc  def  ghi";
30     string abc, def, ghi;
31     string[] overflow;
32     immutable results = line.splitInto(abc, def, ghi, overflow);
33 
34     assert((abc == "abc"), abc);
35     assert((def == "def"), def);
36     assert((ghi == "ghi"), ghi);
37     assert(!overflow.length, overflow.text);
38     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
39 }
40 {
41     string line = "abc_def ghi";
42     string abc, def, ghi;
43     string[] overflow;
44     immutable results = line.splitInto!"_"(abc, def, ghi, overflow);
45 
46     assert((abc == "abc"), abc);
47     assert((def == "def ghi"), def);
48     assert(!ghi.length, ghi);
49     assert(!overflow.length, overflow.text);
50     assert((results == SplitResults.underrun), Enum!SplitResults.toString(results));
51 }
52 {
53     string line = "abc def ghi";
54     string abc, def;
55     string[] overflow;
56     immutable results = line.splitInto(abc, def, overflow);
57 
58     assert((abc == "abc"), abc);
59     assert((def == "def"), def);
60     assert((overflow == [ "ghi" ]), overflow.text);
61     assert((results == SplitResults.overrun), Enum!SplitResults.toString(results));
62 }
63 {
64     string line = "abc///def";
65     string abc, def;
66     string[] overflow;
67     immutable results = line.splitInto!"//"(abc, def, overflow);
68 
69     assert((abc == "abc"), abc);
70     assert((def == "/def"), def);
71     assert(!overflow.length, overflow.text);
72     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
73 }
74 {
75     string line = "abc 123 def I am a fish";
76     string abc, a123, def;
77     string[] overflow;
78     immutable results = line.splitInto(abc, a123, def, overflow);
79 
80     assert((abc == "abc"), abc);
81     assert((a123 == "123"), a123);
82     assert((def == "def"), def);
83     assert((overflow == [ "I", "am", "a", "fish" ]), overflow.text);
84     assert((results == SplitResults.overrun), Enum!SplitResults.toString(results));
85 }
86 {
87     string line = `abc 123 def "I am a fish"`;
88     string abc, a123, def;
89     string[] overflow;
90     immutable results = line.splitInto(abc, a123, def, overflow);
91 
92     assert((abc == "abc"), abc);
93     assert((a123 == "123"), a123);
94     assert((def == "def"), def);
95     assert((overflow == [ "I am a fish" ]), overflow.text);
96     assert((results == SplitResults.overrun), Enum!SplitResults.toString(results));
97 }
98 {
99     string line;
100     string abc, def;
101     string[] overflow;
102     immutable results = line.splitInto(abc, def, overflow);
103     assert((results == SplitResults.underrun), Enum!SplitResults.toString(results));
104 }
105 {
106     string line = "abchonkelonkhonkelodef";
107     string abc, def;
108     string[] overflow;
109     immutable results = line.splitInto!"honkelonk"(abc, def, overflow);
110 
111     assert((abc == "abc"), abc);
112     assert((def == "honkelodef"), def);
113     assert(!overflow.length, overflow.text);
114     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
115 }
116 {
117     string line = "honkelonkhonkelodef";
118     string abc, def;
119     string[] overflow;
120     immutable results = line.splitInto!"honkelonk"(abc, def, overflow);
121 
122     assert((abc == "honkelodef"), abc);
123     assert((def == string.init), def);
124     assert(!overflow.length, overflow.text);
125     assert((results == SplitResults.underrun), Enum!SplitResults.toString(results));
126 }
127 {
128     string line = "###########hirrsteff#snabel";
129     string abc, def;
130     string[] overflow;
131     immutable results = line.splitInto!"#"(abc, def, overflow);
132 
133     assert((abc == "hirrsteff"), abc);
134     assert((def == "snabel"), def);
135     assert(!overflow.length, overflow.text);
136     assert((results == SplitResults.match), Enum!SplitResults.toString(results));
137 }
138 {
139     string line = "abc def ghi";
140     string[] overflow;
141     immutable results = line.splitInto(overflow);
142     immutable expectedOverflow = [ "abc", "def", "ghi" ];
143 
144     assert((overflow == expectedOverflow), overflow.text);
145     assert((results == SplitResults.overrun), Enum!SplitResults.toString(results));
146 }