forked from erlang/eep
/
eep-0031.txt
767 lines (547 loc) · 24.6 KB
/
eep-0031.txt
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
EEP: 31
Title: Binary manipulation and searching module
Version: 1.2
Last-Modified: 18-Dec-2009 12:12
Author: Patrik Nyblom, Fredrik Svahn
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 28-Nov-2009
Erlang-Version: R13B03
Post-History:
Abstract
========
This EEP contains developed suggestions regarding the module ``binary``
first suggested in EEP 9.
EEP 9 suggests several modules and is partially superseded by later
EEP's (i.e. EEP 11), while still containing valuable suggestions not
yet implemented. The remaining modules from EEP 9 will therefore
appear in separate EEP's. This construction is made in agreement with
the original author of EEP 9.
The module ``binary`` is suggested to contain fast searching
algorithms together with some common operations on binaries already
present for lists (in the lists module).
Motivation
==========
While efficient searching is already present in the ``re`` library,
dedicated search functions can further speed up searching in
binaries, given an efficient implementation (i.e. Boyer-More
and Aho-Corassick algorithm). Another important advantage of
separate searching algorithms are ease of use to the programmer,
as the suggested interfaces do not require knowledge about regular
expression syntax and special characters in the binaries need not
be escaped. It's interesting to note how often regular expressions
are used for simple sub-string searching or replacement, which can
with this suggested module be done easily.
Decomposition of binaries are usually done by using bit-syntax.
However some common operations are useful to have as ordinary functions,
both for performance and to support a more traditiona functional
programming style.
Some operations for converting lists to binaries and v.v. are today in
the erlang module. BIFs concerning binaries now present have varied
view of zero vs. one-based positioning in binaries. I.e.
binay_to_list/3 uses one-based while split_binary/2 uses
zero-based. As the convention is to use zero-based, new functions for
convertion binaries to lists and v.v. are needed.
Binaries are in fact a shared data-type, with small binaries often
referencing parts of larger binaries in a way not controllable by
the programmer in a simple way. The bitstring data-type further
complicate things to the programmer in a way hard to easily
manage. I therefore also suggest some low level functions to
inspect binary representation and to clone binaries to ensure a
minimal representation.
As matching is not allowed in guard expressions, I furthermore suggest
that a function for extracting parts of binaries is added to the set
of guard BIFs. This would be consistent with the function element/2
being allowed in guards.
Rationale
=========
For the lists data type there is a help library providing functions for
common operations such as searching and splitting lists. This EEP suggests
that a similar set of library functions should be created for binaries.
Many of the proposed functions are based on answers to questions regarding
binaries on the erlang-questions mailing list, e.g. "how do I convert a
number to a binary?". This EEP therefore suggests the addition of one
module in stdlib, namely a module ``binary`` which will implement the
requested functionality in an efficient way. Most of this module will
need to be implemented in native code (residing in the virtual
machine) why the proposed implementation will be delivered as "beta"
functionality in a forthcoming Erlang release.
The functionality suggested is the following:
- Functionality for searching, splitting and replacing in
binaries. The functionality in some ways will overlap that of the
regular expression library already present in Erlang, but will be
even more efficient and will have a simpler interface.
- Common operations on binaries that have their counterparts for lists
already in the stdlib module ``lists``. While not all interfaces in
the ``lists`` module are applicable to binaries, many are. This module
also provides a good place for future operations on binaries,
operations that are not applicable to lists or that we still don't
know the need for.
- Functions for converting lists to binaries and v.v. These functions
should have a consistent view of zero-based indexing in binaries.
- Operations on binaries concerning their internal
representation. This functionality is sometimes necessary to avoid
extensive use of memory due to the shared nature of the binaries. As
operations on binaries do not involve copying when binaries are
taken apart, programs can unknowingly (or at least unintentionally)
keep references to large binaries by holding seemingly small amounts
of data in the process. The O(1) nature of many operations on
binaries makes the data sharing necessary, but the effects can
sometimes be surprising. On the other hand, O(n) complexity and
instant memory explosions when splitting a binary would be even more
surprising, why the current behavior need to be retained. It is suggested
that functions for both inspecting the nature of sharing of a binary
and to clone a copy of a binary to avoid sharing effects is present
in this suggested module.
All functionality is to be applied to byte oriented binaries, never
bitstrings that do not have a bitlength that is a multiple of
eight. All binaries supplied to and returned by these functions should
pass the is_binary/1 test, otherwise an error will be raised.
Suggested module reference
--------------------------
I suggest the following functionality (presented as an excerpt of an Erlang
manual pages). A discussion about the interface can be found below.
DATA TYPES
..........
**cp()**
Opaque data-type representing a compiled search-pattern.
guaranteed to be a tuple() to allow programs to distinguish it from
non precompiled search patterns.
**part() = {Pos,Length}**
- Start = int()
- Length = int()
A representaion of a part (or range) in a binary. ``Start`` is a
zero-based offset into a binary() and Length is the length of that
part. As input to functions in this module, a reverse part
specification is allowed, constructed with a negative ``Length``, so
that the part of the binary begins at ``Start`` + ``Length`` and is
-``Length`` long. This is useful for referencing the last N bytes
of a binary as ``{size(Binary), -N}``. The functions in this module
always return part()'s with positive ``Length``.
EXPORTS
.......
**compile_pattern(Pattern) -> cp()**
Types:
- Pattern = binary() | [ binary() ]
Builds an internal structure representing a compilation of a
search-pattern, later to be used in the find, split or replace
functions. The cp() returned is guaranteed to be a tuple() to allow
programs to distinguish it from non precompiled search patterns
When a list of binaries is given, it denotes a *set* of alternative
binaries to search for. I.e if ``[<<"functional">>, <<"programming">>]``
is given as ``Pattern``, this means ''either ``<<"functional">>`` *or*
``<<"programming">>``''. The pattern is a *set* of alternatives; when
only a single binary is given, the set has only one element.
If pattern is not a binary or a flat proper list of binaries, a ``badarg``
exception will be raised.
**match(Subject, Pattern) -> Found |** ``no``
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Found = part()
The same as match(Subject, Pattern, []).
**match(Subject,Pattern,Options) -> Found |** ``no``
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Found = part()
- Options = [ Option ]
- Option = {scope, part()}
Searches for the first occurrence of Pattern in Subject and returns
the position and lengt.
The function will return
``{Pos,Length}`` for the binary in ``Pattern`` starting at
the lowest position in ``Subject``, Example::
1> binary:find(<<"abcde">>, [<<"bcde">>,<<"cd">>],[]).
{1,4}
Even though ``<<"cd">>`` ends before ``<<"bcde">>``, ``<<"bcde">>``
begins first and is therefore the first match. If two overlapping
matches begins at the same position, the longest is returned.
Summary of the options:
``{scope, {Start, Length}}``
Only the given part is searched. Return values still have offsets
from the beginning of ``Subject``. A negative ``Length`` is
allowed as described in the **TYPES** section of this manual.
The found part() is returned, if none of the strings in ``Pattern`` is
found, the atom ``no`` is returned.
For a descrition of ``Pattern``, see ``compile_pattern/1``.
If ``{scope, {Start,Length}}`` is given in the options such that
``Start`` is larger than the size of ``Subject``, ``Start`` +
``Length`` is less than zero or ``Start`` + ``Length`` is larger than
the size of ``Subject``, a ``badarg`` exception is raised.
**matches(Subject, Pattern) -> Found**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Found = [ part() ] | []
The same as matches(Subject, Pattern, []).
**matches(Subject,Pattern,Options) -> Found**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Found = [ part() ] | []
- Options = [ Option ]
- Option = {scope, part()}
Works like match, but the ``Subject`` is search until exhausted and
a list of all non-overlapping parts present in Pattern are returned (in order).
The first and *longest* match is preferred
to a shorter, which is illustrated by the following example::
1> binary:matches(<<"abcde">>, [<<"bcde">>,<<"bc">>>,<<"de">>],[]).
[{1,4}]
\- the result shows that ``<<"bcde">>>`` is selected instead of the
shorter match ``<<"bc">>`` (which would have given raise to one more
match,``<<"de">>``). This corresponds to the behavior of
posix regular expressions (and programs like ``awk``), but is not
consistent with alternative matches in ``re`` (and Perl), where
instead lexical ordering in the search pattern selects which string
matches.
If none of the strings in pattern is found, an empty list is returned.
For a descrition of ``Pattern``, see ``compile_pattern/1`` and for a
desctioption of available options, see ``match/3``.
If ``{scope, {Start,Length}}`` is given in the options such that
``Start`` is larger than the size of ``Subject``, ``Start`` +
``Length`` is less than zero or ``Start`` + ``Length`` is larger than
the size of ``Subject``, a ``badarg`` exception is raised.
**split(Subject,Pattern) -> Parts**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Parts = [ binary() ]
The same as split(Subject, Pattern, []).
**split(Subject,Pattern,Options) -> Parts**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Parts = [ binary() ]
- Options = [ Option ]
- Option = {scope, part()} | trim | global
Splits Binary into a list of binaries based on ``Pattern``. If the
option ``global``is not given, only the first occurrence of
``Pattern`` in ``Subject`` will give rise to a split.
The parts of ``Pattern`` actually found in ``Subject`` are not included
in the result.
Example::
1> binary:split(<<1,255,4,0,0,0,2,3>>, [<<0,0,0>>,<<2>>],[]).
[<<1,255,4>>, <<2,3>>]
2> binary:split(<<0,1,0,0,4,255,255,9>>, [<<0,0>>, <<255,255>>],[global]).
[<<0,1>>,<<4>>,<<9>>]
Summary of options:
``{scope, part()}``
Works as in ``binary:match/3`` and ``binary:matches/3``. Note
that this only defines the scope of the search for matching
strings, it does not cut the binary before splitting. The
bytes before and after the scope will be kept in the result. See example below.
``trim``
Removes trailing empty parts of the result (as does ``trim``
in ``re:split/3``)
``global``
Repeats the split until the ``Subject`` is
exhausted. Conceptually the ``global`` option makes ``split``
work on the positions returned by ``binary:matches/3``, while
it normally works on the position returned by
``binary:match/3``.
Example of the difference between a ``scope`` and taking the binary apart
before splitting::
1> binary:split(<<"banana">>,[<<"a">>],[{scope,{2,3}}]).
[<<"ban">>,<<"na">>]
2> binary:split(binary:part(<<"banana">>,{2,3}),[<<"a">>],[]).
[<<"n">>,<<"n">>]
The return type is always a list of binaries which are all referencing
``Subject``. This means that the data in ``Subject`` is not actually
copied to new binaries and that ``Subject`` cannot be garbage
collected until the results of the split are no longer referenced.
For a descrition of ``Pattern``, see ``compile_pattern/1``.
**replace(Subject,Pattern,Replacement) -> Result**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Replacement = binary()
- Result = binary()
The same as replace(Subject,Pattern,Replacement,[]).
**replace(Subject,Pattern,Replacement,Options) -> Result**
Types:
- Subject = binary()
- Pattern = binary() | [ binary() ] | cp()
- Replacement = binary()
- Result = binary()
- Options = [ Option ]
- Option = global | {scope, part()} | {insert_replaced, InsPos}
- InsPos = OnePos | [ OnePos ]
- OnePos = int() =< byte_size(Replacement)
Constructs a new binary by replacing the parts in ``Subject`` matching
``Pattern`` with the content of ``Replacement``.
If the matching sub-part of ``Subject`` giving raise to the
replacement is to be inserted in the result, the option
``{insert_replaced, InsPos}`` will insert the matching part into
``Replacement`` at the given position (or positions) before actually
inserting ``Replacement`` into the Subject. Example::
1> binary:replace(<<"abcde">>,<<"b">>,<<"[]">>,[{insert_replaced,1}]).
<<"a[b]cde">>
2> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
[global,{insert_replaced,1}]).
<<"a[b]c[d]e">>
3> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
[global,{insert_replaced,[1,1]}]).
<<"a[bb]c[dd]e">>
4> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[-]">>,
[global,{insert_replaced,[1,2]}]).
<<"a[b-b]c[d-d]e">>
If any position given in InsPos is greater than the size of the replacement binary, a
``badarg`` exception is raised.
The options ``global`` and ``{scope, part()}`` works as for
``binary:split/3``. The return type is always a binary.
For a descrition of ``Pattern``, see ``compile_pattern/1``.
**longest_common_prefix(Binaries) -> int()**
Types:
- Binaries = [ binary() ]
Returns the length of the longest common prefix of the binaries in the
list ``Binaries``. Example::
1> binary:longest_common_prefix([<<"erlang">>,<<"ergonomy">>]).
2
2> binary:longest_common_prefix([<<"erlang">>,<<"perl">>]).
0
If ``Binaries`` is not a flat list of binaries, a ``badarg`` exception
is raised.
**longest_common_suffix(Binaries) -> int()**
Types:
- Binaries = [ binary() ]
Returns the length of the longest common suffix of the binaries in the
list ``Binaries``. Example::
1> binary:longest_common_suffix([<<"erlang">>,<<"fang">>]).
3
2> binary:longest_common_suffix([<<"erlang">>,<<"perl">>]).
0
If ``Binaries`` is not a flat list of binaries, a ``badarg`` exception
is raised.
**first(Subject) -> int()**
Types:
- Subject = binary()
Returns the first byte of the binary as an integer. If the binary
length is zero, a ``badarg`` exception is raised.
**last(Subject) -> int()**
Types:
- Subject = binary()
Returns the last byte of the binary as an integer. If the binary
length is zero, a ``badarg`` exception is raised.
**at(Subject, Pos) -> int()**
Types:
- Subject = binary()
- Pos = int() >= 0
Returns the byte at position ``Pos`` (zero-based) in the binary
``Subject`` as an integer. If ``Pos`` >= byte_size(Subject), a
``badarg`` exception is raised.
**part(Subject, PosLen) -> binary()**
Types:
- Subject = binary()
- PosLen = part()
Extracts the part of the binary described by ``PosLen``.
Negative length can be used to extract bytes at the end of a binary::
1> Bin = <<1,2,3,4,5,6,7,8,9,10>>.
2> binary:part(Bin,{byte_size(Bin), -5)).
<<6,7,8,9,10>>
If ``PosLen`` in any way references outside the binary, a ``badarg``
exception is raised.
**part(Subject, Pos, Len) -> binary()**
Types:
- Subject = binary()
- Pos = int()
- Len = int()
The same as part(Subject, {Pos, Len}).
**bin_to_list(Subject) -> list()**
Types:
- Subject = binary()
The same as bin_to_list(Subject,{0,byte_size(Subject)}).
**bin_to_list(Subject, PosLen) -> list()**
- Subject = binary()
- PosLen = part()
Converts ``Subject`` to a list of int(), each int representing the
value of one byte. The ``part()`` denotes which part of the
``binary()`` to convert. Example::
1> binary:bin_to_list(<<"erlang">>,{1,3}).
"rla"
%% or [114,108,97] in list notation.
If ``PosLen`` in any way references outside the binary, a ``badarg``
exception is raised.
**bin_to_list(Subject, Pos, Len) -> list()**
Types:
- Subject = binary()
- Pos = int()
- Len = int()
The same as bin_to_list(Subject,{Pos,Len}).
**list_to_bin(ByteList) -> binary()**
Types:
- ByteList = iodata() (see module erlang)
Works exactly like erlang:list_to_binary/1, added for completeness.
**copy(Subject) -> binary()**
Types:
- Subject = binary()
The same as copy(Subject, 1).
**copy(Subject,N) -> binary()**
Types:
- Subject = binary()
- N = int() >= 0
Creates a binary with the content of ``Subject`` duplicated ``N``
times.
This function will always create a new binary, even if ``N`` = 1. By
using ``copy/1`` on a binary referencing a larger binary, one might
free up the larger binary for garbage collection.
NOTE! By deliberately copying a single binary to avoid referencing a
larger binary, one might, instead of freeing up the larger binary for
later garbage collection, create much more binary data than
needed. Sharing binary data is usually good. Only in special cases,
when small parts reference large binaries and the large binaries are
no longer used *in any process*, deliberate copying might be a good idea.
If ``N`` < 0, a ``badarg`` exception is raised.
**referenced_byte_size(binary()) -> int()**
If a binary references a larger binary (often described as being a
sub-binary), it can be useful to get the size of the actual referenced
binary. This function can be used in a program to trigger the
use of ``copy/1``. By copying a binary, one might dereference the
original, possibly large, binary which a smaller binary is a reference
to.
Example::
store(Binary, GBSet) ->
NewBin =
case binary:referenced_byte_size(Binary) of
Large when Large > 2 * byte_size(Binary) ->
binary:copy(Binary);
_ ->
Binary
end,
gb_sets:insert(NewBin,GBSet).
In this example, we chose to copy the binary content before inserting
it in the gb_set() if it references a binary more than twice the size
of the data we're going to keep. Of course different rules for when
copying will apply to different programs.
Binary sharing will occur whenever binaries are taken apart, this is
the fundamental reason why binaries are fast, decomposition can always
be done with O(1) complexity. In rare circumstances this data sharing
is however undesirable, why this function together with ``copy/1``
might be useful when optimizing for memory use.
Example of binary sharing::
1> A = binary:copy(<<1>>,100).
<<1,1,1,1,1 ...
2> byte_size(A).
100
3> binary:referenced_byte_size(A)
100
4> <<_:10/binary,B:10/binary,_/binary>> = A.
<<1,1,1,1,1 ...
5> byte_size(B).
10
6> binary:referenced_byte_size(B)
100
NOTE! Binary data is shared among processes. If another process still
references the larger binary, copying the part this process uses only
consumes more memory and will not free up the larger binary for garbage
collection. Use this kind of intrusive functions with extreme care,
and only if a *real* problem is detected.
**encode_unsigned(Unsigned) -> binary()**
Types:
- Unsigned = int() >= 0
The same as encode_unsigned(Unsigned,big).
**encode_unsigned(Unsigned,Endianess) -> binary()**
Types:
- Unsigned = int() >= 0
- Endianess = big | little
Converts a positive integer the smallest possible representation
in in a binary digit representation, either big or little endian.
Example::
1> binary:encode_unsigned(11111111,big).
<<169,138,199>>
**decode_unsigned(Subject) -> Unsigned**
Types:
- Subject = binary()
- Unsigned = int() >= 0
The same as encode_unsigned(Subject,big).
**decode_unsigned(Subject, Endianess) -> Unsigned**
Types:
- Subject = binary()
- Endianess = big | little
- Unsigned = int() >= 0
Converts the binary digit representation, in big or little endian, of
a positive integer in Subject to an Erlang int().
Example::
1> binary:decode_unsigned(<<169,138,199>>,big).
11111111
Guard BIF
---------
I suggest adding the functions binary:part/2 and binary:part/3 to the
set of BIFs allowed in guard tests. As guard BIFs are traditionally
put in the erlang module, the following names for the guard BIFs are
suggested::
erlang:binary_part/2
erlang:binary_part/3
They should both work exactly as their counterparts in the binary module.
Interface design discussion
---------------------------
As with all modules, there are a lot of arguments about the actual
interface, sometimes more than about the functionality. In this case a
number of parameters has to be considered.
- Effectiveness - The interface should be constructed so that fast
implementation is possible and so that code using the interface can
be written in an effective way. To not create unnecessary garbage is
one parameter, to allow for general code is another.
- Parameter ordering - I've chosen to make the binary subject the
first parameter in all applicable calls. Putting the subject first
corresponds to the ``re`` interface. The ``lists`` module, however,
usually has the subject as last parameter. We could go for that
instead, but unfortunately the ``lists:sublist/{2,3}`` interface,
which corresponds to the ``part`` function, has the subject
first, why following the conventions of ``lists`` would not only
break conformance with ``re``, it would also give us a generally
non-stringent interface. The effect of not conforming to the
``lists`` interface is that using function names from that module
would lead to confusion and therefore is avoided.
- Function naming - We have two related modules to take into account
when naming functions here. The module ``re`` is related to the
searching function (``match``, ``replace`` etc), while the ``lists``
module is related to the decomposition functions (``first``,
``last`` etc).
I've basically retained the names from ``re`` when I find the
functionality, both in concept and interface to be similar
enough. The nature of regular expressions as small executable
programs, which is to much to say for a collection of binaries as
the patterns are in this module, prohibits the use of the function
name ``run`` for actually doing the searching. We use ``match`` and
``matches`` instead of ``run``.
As this module is more general than ``re``, a function name like
``compile`` is not really good. ``re:compile`` means "compile a
regular expression", but what would ``binary:compile`` mean?
Therefore the pre-processing function is instead called
``compile_pattern``.
When it comes to the ``lists`` module, the parameter ordering has
prevented me from reusing any function names but ``last``, which
only takes one parameter in ``lists`` and there is no real
alternative there.
- Options or multiple functions - I believe a good rule of thumb is to
not have options that change the return type of the function, which
would have been the case if we i.e. had a ``global`` option to
``match/3`` instead of a separate ``matches/3`` function.
The fact that there are a manageable set of possible return types
for the searching and decomposition functiona allows us to follow
that rule of thumb.
(Unfortunately that rule could not be easilly followed in ``re``, as the
rich assortment of options would have given rise to a non-manageable
amount of function names).
Performance
===========
Although the decomposition functions are not really faster than using
bit-syntax for decomposition, they create slightly less garbage than
the bit syntax. As they are not slower than bit-syntax, they also have
a purpose in allowing for a different programming style.
The match/replace/split functionality should be compared to similar
functionality in the ``re`` module. Implementation methods has to be
chosen so that this modules search functions are faster, or possibly
even significantly faster, than ``re``.
Reference implementation
========================
A reference implementation will be included as beta functionality in
R13B04. Prereleases may be available via github.
References
==========
[1] EEP 9, the original work from which this EEP is derived.
Copyright
=========
This document is licensed under the Creative Commons license.