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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2009. 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).
%%
%% The following functions implemented as BIF's
%% binary:compile_pattern/1
%% binary:find/3
%% XXX:PaN more to come...
-export([first/1,first/2,last/1,last/2,nth/2,extract/3]).
first(<<F:1/binary,_/binary>>) ->
F;
first(_) ->
erlang:error(badarg).
first(N,Bin) when is_integer(N), N >= 0 ->
case Bin of
<<F:N/binary,_/binary>> ->
F;
_ ->
erlang:error(badarg)
end;
first(_,_) ->
erlang:error(badarg).
last(<<>>) ->
erlang:error(badarg);
last(Bin) when is_binary(Bin) ->
Sz = byte_size(Bin) - 1,
<<_:Sz/binary,L:1/binary>> = Bin,
L;
last(_) ->
erlang:error(badarg).
last(N,Bin) when is_integer(N), N >= 0, is_binary(Bin) ->
Sz = byte_size(Bin) - N,
case Bin of
<<_:Sz/binary,L:N/binary>> ->
L;
_ ->
erlang:error(badarg)
end;
last(_,_) ->
erlang:error(badarg).
nth(N, Bin) when is_integer(N), N > 0 ->
M = N - 1,
case Bin of
<<_:M/binary,V:1/binary,_/binary>> ->
V;
_ ->
erlang:error(badarg)
end;
nth(_,_) ->
erlang:error(badarg).
extract(N,Size,Bin) when is_integer(N), is_integer(Size), is_binary(Bin) ->
M = N - 1,
case Bin of
<<_:M/binary,V:Size/binary,_/binary>> ->
V;
_ ->
erlang:error(badarg)
end;
extract(_,_,_) ->
erlang:error(badarg).
|