File: README.md

package info (click to toggle)
node-bufferjs 3.1.0%2Bgit20160608.605b357%2B~cs0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 280 kB
  • sloc: javascript: 189; makefile: 14; sh: 11
file content (101 lines) | stat: -rw-r--r-- 2,524 bytes parent folder | download
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
node-bufferjs
====

Pure JavaScript utils which extend the global `Buffer` object in Node.JS.

    npm install bufferjs

API
====

    require('bufferjs');

  * buffer.addChunk(chunk)
  * Buffer.indexOf(haystack, needle, startIndex)
  * buffer.indexOf(needle, startIndex)

buffer.addChunk(chunk)
----

Adds a buffer `chunk` to `buffer`.

Returns `true` when full

Returns `new Buffer` if buffer overflows

    require('bufferjs/add-chunk');

    var buffer = new Buffer(9),
      overflow;

    buffer.addChunk(new Buffer("abc"));
    buffer.addChunk(new Buffer("def"));
    buffer.toString(); // "abcdef???"
    // Note: "???" is whatever random garbage was internally `malloc`d.    


    console.log(true === buffer.addChunk(new Buffer("ghi"))); // true, buffer is full
    // true when buffer is full


    // adding past the index will throw an error
    overflow = buffer.addChunk(new Buffer("jkl"));
    buffer.toString(); // abcdefghi
    overflow.toString(); // jkl


Buffer.indexOf(haystack, needle, startIndex)
----

Searches the given _haystack_ Buffer for the given _needle_ Buffer. Returns the index
of the first occurence of `needle`, or `-1` if the pattern was not found. `needle` may also
be a regular String, for convenience. `startIndex` tells at which index to begin the search.

    require('bufferjs/indexOf');

    var haystack = new Buffer(10);
    var needle = '\r\n';
    haystack.write(needle, 5);
    
    Buffer.indexOf(haystack, needle); // 5
    Buffer.indexOf(haystack, 'NOT_IN_HAYSTACK'); // -1


buffer.indexOf(needle, startIndex)
----

OO-style variant of `Buffer.indexOf`. `this` is the _haystack_, and only _needle_ needs
to be supplied when called.

    require('bufferjs/indexOf');
  
    var haystack = new Buffer(10);
    var needle = '\r\n';
    haystack.write(needle, 5);

    haystack.indexOf(needle); // 5
    haystack.indexOf('NOT_IN_HAYSTACK'); // -1

Buffer.concat(list, [totalLength])
----

**Deprecated**: Node.JS 0.8.x has Buffer.concat built-in (compatible).
See http://nodejs.org/api/buffer.html#buffer_class_method_buffer_concat_list_totallength

**WARNING**: v1.2.1 uses NON-NATIVE concat. v2.0.0 uses NATIVE concat ONLY.

Returns a new `Buffer` with the contents of all buffers in the `Array`.

    require('bufferjs/concat');

    var buffers = [
            new Buffer("abc")
          , new Buffer("def")
          , new Buffer("ghi")
        ]
      , buffer
      ;

    buffer = Buffer.concat(buffers);
    console.log(buffer.toString('utf8'));
    // abcdefghi