2021-01-12 14:29:47 +08:00
|
|
|
## 读缓冲区扩展函数
|
|
|
|
|
2021-01-14 09:23:11 +08:00
|
|
|
rbuffer 有以下属性:
|
|
|
|
|
|
|
|
* data 缓冲区的指针
|
|
|
|
* cursor 当前读的位置
|
|
|
|
* capacity 最大容量
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 1.rbuffer\_create
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 创建 rbuffer 对象。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_create(str) => object
|
|
|
|
rbuffer_create(binary) => object
|
|
|
|
rbuffer_create(data, size) => object
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 2.rbuffer\_skip
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 跳过指定长度的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_skip(rbuffer, size) => bool
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_skip(a, 2));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 3.rbuffer\_rewind
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 重置读取指针到 0。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_rewind(rbuffer) => bool
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_skip(a, 2));
|
|
|
|
print(rbuffer_rewind(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 4.rbuffer\_read\_uint8
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 uint8_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_uint8(rbuffer) => uint8_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_uint8(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 5.rbuffer\_read\_uint16
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 uint16_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_uint16(rbuffer) => uint16_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_uint16(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 6.rbuffer\_read\_uint32
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 uint32_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_uint32(rbuffer) => uint32_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_uint32(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 7.rbuffer\_read\_uint64
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 uint64_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_uint64(rbuffer) => uint64_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_uint64(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 8.rbuffer\_read\_int8
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 int8_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_int8(rbuffer) => int8_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_int8(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 9.rbuffer\_read\_int16
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 int16_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_int16(rbuffer) => int16_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_int16(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 10.rbuffer\_read\_int32
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 int32_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_int32(rbuffer) => int32_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_int32(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 11.rbuffer\_read\_int64
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 int64_t 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_int64(rbuffer) => int64_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_int64(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 12.rbuffer\_read\_float
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 float 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_float(rbuffer) => float
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_float(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 13.rbuffer\_read\_double
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取 double 类型的数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_double(rbuffer) => double
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_double(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 14.rbuffer\_read\_string
|
2021-01-13 17:39:21 +08:00
|
|
|
|
2021-07-13 16:35:16 +08:00
|
|
|
> 读取字符串。
|
2021-01-13 17:39:21 +08:00
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
2021-07-13 16:35:16 +08:00
|
|
|
rbuffer_read_string(rbuffer) => str
|
2021-01-13 17:39:21 +08:00
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_string(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 15.rbuffer\_read\_binary
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 读取指定长度的二进制数据。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_read_binary(rbuffer, size) => binary
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_read_binary(a, 5));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 16.rbuffer\_get\_data
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 获取 buffer 的指针。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_get_data(rbuffer) => pointer
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
|
|
|
var b = rbuffer_get_data(a);
|
2022-08-23 10:12:36 +08:00
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 17.rbuffer\_get\_cursor
|
2021-01-13 17:39:21 +08:00
|
|
|
|
|
|
|
> 获取 buffer 当前的读取位置。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_get_cursor(rbuffer) => uint32_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_get_cursor(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 18.rbuffer\_get\_capacity
|
|
|
|
|
2021-01-13 17:39:21 +08:00
|
|
|
> 获取 buffer 的容量。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
rbuffer_get_capacity(rbuffer) => uint32_t
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = rbuffer_create("hello");
|
2022-08-23 10:12:36 +08:00
|
|
|
print(rbuffer_get_capacity(a));
|
|
|
|
```
|
|
|
|
|
2021-01-12 14:29:47 +08:00
|
|
|
### 更多示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
...
|
|
|
|
|
2022-08-23 18:27:17 +08:00
|
|
|
var b=rbuffer_create(wbuffer_get_data(a), wbuffer_get_cursor(a))
|
2021-01-12 14:29:47 +08:00
|
|
|
|
|
|
|
assert(rbuffer_get_cursor(b)==0)
|
|
|
|
assert(rbuffer_read_int8(b)== -10)
|
|
|
|
|
|
|
|
assert(rbuffer_get_cursor(b)==1)
|
|
|
|
assert(rbuffer_read_int16(b)==-1122)
|
|
|
|
|
|
|
|
assert(rbuffer_get_cursor(b)==3)
|
|
|
|
assert(rbuffer_read_int32(b)==-11223344)
|
|
|
|
|
|
|
|
assert(rbuffer_get_cursor(b)==7)
|
|
|
|
assert(rbuffer_read_int64(b)==-1122334455667788)
|
|
|
|
|
|
|
|
assert(rbuffer_get_cursor(b)==15)
|
|
|
|
assert(rbuffer_read_string(b)=="hello")
|
|
|
|
assert(rbuffer_get_cursor(b)==21)
|
2022-08-23 18:27:17 +08:00
|
|
|
var c = rbuffer_read_binary(b, 6)
|
2021-01-12 14:29:47 +08:00
|
|
|
assert(rbuffer_get_cursor(b)==27)
|
|
|
|
|
|
|
|
wbuffer_rewind(a)
|
|
|
|
assert(wbuffer_get_cursor(a)==0)
|
|
|
|
wbuffer_write_binary(a, c)
|
|
|
|
assert(wbuffer_get_cursor(a)==6)
|
|
|
|
|
|
|
|
unset(a)
|
|
|
|
unset(b)
|
2022-08-23 18:27:17 +08:00
|
|
|
unset(c)
|
2021-01-12 14:29:47 +08:00
|
|
|
```
|