comparison scripts/strings/strjoin.m @ 16815:7a97ff5ef42e

strjoin() should return an empty [0x0] string for an empty input. * scripts/strings/strjoin.m: Return [0x0] char for an empty input. Add test.
author Ben Abbott <bpabbott@mac.com>
date Sun, 23 Jun 2013 09:05:05 +0800
parents e2de3c8882be
children 12005245b645
comparison
equal deleted inserted replaced
16814:64e7bb01fce2 16815:7a97ff5ef42e
68 68
69 num = numel (cstr); 69 num = numel (cstr);
70 if (numel (delimiter) == 1 && num > 1) 70 if (numel (delimiter) == 1 && num > 1)
71 delimiter = repmat (delimiter, 1, num); 71 delimiter = repmat (delimiter, 1, num);
72 delimiter(end) = {""}; 72 delimiter(end) = {""};
73 elseif (numel (delimiter) != num - 1) 73 elseif (num > 0 && numel (delimiter) != num - 1)
74 error ("strjoin:cellstring_delimiter_mismatch", 74 error ("strjoin:cellstring_delimiter_mismatch",
75 "strjoin: the number of delimiters does not match the number of strings") 75 "strjoin: the number of delimiters does not match the number of strings")
76 else 76 else
77 delimiter(end+1) = {""}; 77 delimiter(end+1) = {""};
78 endif 78 endif
79 79
80 rval = [[cstr(:).'; delimiter(:).']{:}]; 80 if (num == 0)
81 rval = ""
82 else
83 rval = [[cstr(:).'; delimiter(:).']{:}];
84 endif
81 85
82 endfunction 86 endfunction
83 87
84 %!assert (strjoin ({"hello"}, "-"), "hello") 88 %!assert (strjoin ({"hello"}, "-"), "hello")
85 %!assert (strjoin ({"hello", "world"}), "hello world") 89 %!assert (strjoin ({"hello", "world"}), "hello world")
87 %! "Octave*Scilab*Lush*Yorick") 91 %! "Octave*Scilab*Lush*Yorick")
88 %!assert (strjoin ({"space", "comma", "dash", "semicolon", "done"}, 92 %!assert (strjoin ({"space", "comma", "dash", "semicolon", "done"},
89 %! {" ", ",", "-", ";"}), "space comma,dash-semicolon;done") 93 %! {" ", ",", "-", ";"}), "space comma,dash-semicolon;done")
90 %!assert (strjoin ({'Octave','Scilab'},'\n'), "Octave\nScilab") 94 %!assert (strjoin ({'Octave','Scilab'},'\n'), "Octave\nScilab")
91 %!assert (strjoin ({'Octave','Scilab'},{'\n'}), "Octave\\nScilab") 95 %!assert (strjoin ({'Octave','Scilab'},{'\n'}), "Octave\\nScilab")
96 %!assert (strjoin ({},'foo'), "")