1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(binary).
%%
%% Implemented in this module:
-export([split/2,split/3,replace/3,replace/4]).
-export_type([cp/0]).
-opaque cp() :: {'am' | 'bm', binary()}.
-type part() :: {Start :: non_neg_integer(), Length :: integer()}.
%%% BIFs.
-export([at/2, bin_to_list/1, bin_to_list/2, bin_to_list/3,
compile_pattern/1, copy/1, copy/2, decode_unsigned/1,
decode_unsigned/2, encode_unsigned/1, encode_unsigned/2,
first/1, last/1, list_to_bin/1, longest_common_prefix/1,
longest_common_suffix/1, match/2, match/3, matches/2,
matches/3, part/2, part/3, referenced_byte_size/1]).
-spec at(Subject, Pos) -> byte() when
Subject :: binary(),
Pos :: non_neg_integer().
at(_, _) ->
erlang:nif_error(undef).
-spec bin_to_list(Subject) -> [byte()] when
Subject :: binary().
bin_to_list(_) ->
erlang:nif_error(undef).
-spec bin_to_list(Subject, PosLen) -> [byte()] when
Subject :: binary(),
PosLen :: part().
bin_to_list(_, _) ->
erlang:nif_error(undef).
-spec bin_to_list(Subject, Pos, Len) -> [byte()] when
Subject :: binary(),
Pos :: non_neg_integer(),
Len :: integer().
bin_to_list(_, _, _) ->
erlang:nif_error(undef).
-spec compile_pattern(Pattern) -> cp() when
Pattern :: binary() | [binary()].
compile_pattern(_) ->
erlang:nif_error(undef).
-spec copy(Subject) -> binary() when
Subject :: binary().
copy(_) ->
erlang:nif_error(undef).
-spec copy(Subject, N) -> binary() when
Subject :: binary(),
N :: non_neg_integer().
copy(_, _) ->
erlang:nif_error(undef).
-spec decode_unsigned(Subject) -> Unsigned when
Subject :: binary(),
Unsigned :: non_neg_integer().
decode_unsigned(_) ->
erlang:nif_error(undef).
-spec decode_unsigned(Subject, Endianess) -> Unsigned when
Subject :: binary(),
Endianess :: big | little,
Unsigned :: non_neg_integer().
decode_unsigned(_, _) ->
erlang:nif_error(undef).
-spec encode_unsigned(Unsigned) -> binary() when
Unsigned :: non_neg_integer().
encode_unsigned(_) ->
erlang:nif_error(undef).
-spec encode_unsigned(Unsigned, Endianess) -> binary() when
Unsigned :: non_neg_integer(),
Endianess :: big | little.
encode_unsigned(_, _) ->
erlang:nif_error(undef).
-spec first(Subject) -> byte() when
Subject :: binary().
first(_) ->
erlang:nif_error(undef).
-spec last(Subject) -> byte() when
Subject :: binary().
last(_) ->
erlang:nif_error(undef).
-spec list_to_bin(ByteList) -> binary() when
ByteList :: iodata().
list_to_bin(_) ->
erlang:nif_error(undef).
-spec longest_common_prefix(Binaries) -> non_neg_integer() when
Binaries :: [binary()].
longest_common_prefix(_) ->
erlang:nif_error(undef).
-spec longest_common_suffix(Binaries) -> non_neg_integer() when
Binaries :: [binary()].
longest_common_suffix(_) ->
erlang:nif_error(undef).
-spec match(Subject, Pattern) -> Found | nomatch when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Found :: part().
match(_, _) ->
erlang:nif_error(undef).
-spec match(Subject, Pattern, Options) -> Found | nomatch when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Found :: part(),
Options :: [Option],
Option :: {scope, part()}.
match(_, _, _) ->
erlang:nif_error(undef).
-spec matches(Subject, Pattern) -> Found when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Found :: [part()].
matches(_, _) ->
erlang:nif_error(undef).
-spec matches(Subject, Pattern, Options) -> Found when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Found :: [part()],
Options :: [Option],
Option :: {scope, part()}.
matches(_, _, _) ->
erlang:nif_error(undef).
-spec part(Subject, PosLen) -> binary() when
Subject :: binary(),
PosLen :: part().
part(_, _) ->
erlang:nif_error(undef).
-spec part(Subject, Pos, Len) -> binary() when
Subject :: binary(),
Pos :: non_neg_integer(),
Len :: integer().
part(_, _, _) ->
erlang:nif_error(undef).
-spec referenced_byte_size(Binary) -> non_neg_integer() when
Binary :: binary().
referenced_byte_size(_) ->
erlang:nif_error(undef).
%%% End of BIFs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% split
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec split(Subject, Pattern) -> Parts when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Parts :: [binary()].
split(H,N) ->
split(H,N,[]).
-spec split(Subject, Pattern, Options) -> Parts when
Subject :: binary(),
Pattern :: binary() | [binary()] | cp(),
Options :: [Option],
Option :: {scope, part()} | trim | global,
Parts :: [binary()].
split(Haystack,Needles,Options) ->
try
{Part,Global,Trim} = get_opts_split(Options,{no,false,false}),
Moptlist = case Part of
no ->
[];
{A,B} ->
[{scope,{A,B}}]
end,
MList = if
Global ->
binary:matches(Haystack,Needles,Moptlist);
true ->
case binary:match(Haystack,Needles,Moptlist) of
nomatch -> [];
Match -> [Match]
end
end,
do_split(Haystack,MList,0,Trim)
catch
_:_ ->
erlang:error(badarg)
end.
do_split(H,[],N,true) when N >= byte_size(H) ->
[];
do_split(H,[],N,_) ->
[binary:part(H,{N,byte_size(H)-N})];
do_split(H,[{A,B}|T],N,Trim) ->
case binary:part(H,{N,A-N}) of
<<>> ->
Rest = do_split(H,T,A+B,Trim),
case {Trim, Rest} of
{true,[]} ->
[];
_ ->
[<<>> | Rest]
end;
Oth ->
[Oth | do_split(H,T,A+B,Trim)]
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% replace
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec replace(Subject, Pattern, Replacement) -> Result when
Subject :: binary(),
Pattern :: binary() | [ binary() ] | cp(),
Replacement :: binary(),
Result :: binary().
replace(H,N,R) ->
replace(H,N,R,[]).
-spec replace(Subject, Pattern, Replacement, Options) -> Result when
Subject :: binary(),
Pattern :: binary() | [ binary() ] | cp(),
Replacement :: binary(),
Options :: [Option],
Option :: global | {scope, part()} | {insert_replaced, InsPos},
InsPos :: OnePos | [ OnePos ],
OnePos :: non_neg_integer(),
Result :: binary().
replace(Haystack,Needles,Replacement,Options) ->
try
true = is_binary(Replacement), % Make badarg instead of function clause
{Part,Global,Insert} = get_opts_replace(Options,{no,false,[]}),
Moptlist = case Part of
no ->
[];
{A,B} ->
[{scope,{A,B}}]
end,
MList = if
Global ->
binary:matches(Haystack,Needles,Moptlist);
true ->
case binary:match(Haystack,Needles,Moptlist) of
nomatch -> [];
Match -> [Match]
end
end,
ReplList = case Insert of
[] ->
Replacement;
Y when is_integer(Y) ->
splitat(Replacement,0,[Y]);
Li when is_list(Li) ->
splitat(Replacement,0,lists:sort(Li))
end,
erlang:iolist_to_binary(do_replace(Haystack,MList,ReplList,0))
catch
_:_ ->
erlang:error(badarg)
end.
do_replace(H,[],_,N) ->
[binary:part(H,{N,byte_size(H)-N})];
do_replace(H,[{A,B}|T],Replacement,N) ->
[binary:part(H,{N,A-N}),
if
is_list(Replacement) ->
do_insert(Replacement, binary:part(H,{A,B}));
true ->
Replacement
end
| do_replace(H,T,Replacement,A+B)].
do_insert([X],_) ->
[X];
do_insert([H|T],R) ->
[H,R|do_insert(T,R)].
splitat(H,N,[]) ->
[binary:part(H,{N,byte_size(H)-N})];
splitat(H,N,[I|T]) ->
[binary:part(H,{N,I-N})|splitat(H,I,T)].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Simple helper functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_opts_split([],{Part,Global,Trim}) ->
{Part,Global,Trim};
get_opts_split([{scope,{A,B}} | T],{_Part,Global,Trim}) ->
get_opts_split(T,{{A,B},Global,Trim});
get_opts_split([global | T],{Part,_Global,Trim}) ->
get_opts_split(T,{Part,true,Trim});
get_opts_split([trim | T],{Part,Global,_Trim}) ->
get_opts_split(T,{Part,Global,true});
get_opts_split(_,_) ->
throw(badopt).
get_opts_replace([],{Part,Global,Insert}) ->
{Part,Global,Insert};
get_opts_replace([{scope,{A,B}} | T],{_Part,Global,Insert}) ->
get_opts_replace(T,{{A,B},Global,Insert});
get_opts_replace([global | T],{Part,_Global,Insert}) ->
get_opts_replace(T,{Part,true,Insert});
get_opts_replace([{insert_replaced,N} | T],{Part,Global,_Insert}) ->
get_opts_replace(T,{Part,Global,N});
get_opts_replace(_,_) ->
throw(badopt).
|