Skip to content

Commit 450c1fa

Browse files
committed
add stdlib.append()
More efficient and more broadly available than builtin append()
1 parent f2ab873 commit 450c1fa

File tree

18 files changed

+45
-28
lines changed

18 files changed

+45
-28
lines changed

+stdlib/+sys/device.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
% Get-CimInstance works, but is 100x slower
1818
% c0 = 'pwsh -c "(Get-CimInstance -ClassName Win32_Volume -Filter \"DriveLetter = ''';
1919
% c2 = '''\").SerialNumber"';
20-
% cmd = append(c0, rn, c2);
20+
% cmd = stdlib.append(c0, rn, c2);
2121

2222
cmd = sprintf('vol "%s"', rn);
2323
elseif ismac()

+stdlib/absolute.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
else
3232
c = pwd();
3333
if ~stdlib.strempty(base)
34-
c = append(c, '/', base);
34+
c = stdlib.append(c, '/', base);
3535
end
3636
end
3737

3838
if ~stdlib.strempty(p)
3939
if endsWith(c, ["/", filesep])
40-
c = append(c, p);
40+
c = stdlib.append(c, p);
4141
else
42-
c = append(c, '/', p);
42+
c = stdlib.append(c, '/', p);
4343
end
4444
end
4545

+stdlib/append.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%% APPEND Concatenate strings or character arrays without removing trailing whitespace
2+
% APPEND(S1, S2, ...) appends strings or character vectors S1, S2, ... together.
3+
% the output is a scalar string or character vector
4+
%
5+
% builtin append() is for Matlab >= R2019a, while this works back to R2016b.
6+
% strcat() has the side effect of trimming whitespace, so we prefer
7+
% stdlib.append() for string concatenation in case a user path has trailing whitespace.
8+
9+
function s = append(txt, varargin)
10+
11+
s = string(txt);
12+
s = s.append(varargin{:});
13+
14+
if ischar(txt) && all(cellfun(@ischar, varargin))
15+
s = char(s);
16+
end
17+
18+
end

+stdlib/expanduser.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
e = string(e);
3030
end
3131
else
32-
e = append(home, '/', extractAfter(file, i1));
32+
e = stdlib.append(home, '/', extractAfter(file, i1));
3333
end
3434

3535
end

+stdlib/filename.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
switch backend
1515
case 'fileparts'
1616
[~, f, ext] = fileparts(p);
17-
f = append(f, ext);
17+
f = stdlib.append(f, ext);
1818
% the pattern backend is a litle slower than regexp. Commented out for < R2020b compatibility
1919
% case 'pattern'
2020
% f = extractAfter(p, asManyOfPattern(wildcardPattern + ("/" | filesep)));

+stdlib/java_run.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152

153153
line = reader.readLine();
154154
while ~isempty(line)
155-
msg = append(msg, string(line), newline);
155+
msg = stdlib.append(msg, string(line), newline);
156156
line = reader.readLine();
157157
end
158158
msg = strip(msg);

+stdlib/join.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
if ~stdlib.strempty(rnb)
1616
if endsWith(rnb, ["/", filesep])
17-
p = append(rnb, other);
17+
p = stdlib.append(rnb, other);
1818
else
19-
p = append(rnb, '/', other);
19+
p = stdlib.append(rnb, '/', other);
2020
end
2121
else
2222
p = other;
@@ -26,9 +26,9 @@
2626

2727
if ~stdlib.strempty(other)
2828
if endsWith(base, ["/", filesep])
29-
p = append(base, other);
29+
p = stdlib.append(base, other);
3030
else
31-
p = append(base, '/', other);
31+
p = stdlib.append(base, '/', other);
3232
end
3333
else
3434
p = base;

+stdlib/normalize.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
if n == ""
2727
n = parts(i);
2828
elseif ismember(n, ["/", filesep])
29-
n = append(n, parts(i));
29+
n = stdlib.append(n, parts(i));
3030
else
31-
n = append(n, '/', parts(i));
31+
n = stdlib.append(n, '/', parts(i));
3232
end
3333
end
3434
end

+stdlib/parent.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
p = string(p);
1919
end
2020
elseif ispc() && strcmp(p, stdlib.root_name(file))
21-
p = append(p, '/');
21+
p = stdlib.append(p, '/');
2222
end
2323

2424
end

+stdlib/root.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
function r = root(p)
55

6-
r = append(stdlib.root_name(p), stdlib.root_dir(p));
6+
r = stdlib.append(stdlib.root_name(p), stdlib.root_dir(p));
77

88
end

0 commit comments

Comments
 (0)