Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for new varint and bitflags types #54

Merged
merged 9 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions doc/datatypes/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ They default to big-endian encoding. To use little-endian, prefix its name with
| f64 | 8 | 4.5 | double |
| i64 | 8 | 1 | long |
| u64 | 8 | 1 | unsigned long |
| varint | (varies) | 300 | int var |
| varint | 1-4 | 300 | int var |
| varint64 | 1-8 | 300n | unsigned long |
| varint128 | 1-16 | 2 ^ 68 | unsigned __int128 |
| zigzag32 | 1-4 | -100 | signed int var |
| zigzag64 | 1-8 | -680n | signed long |

### **int** ({ size: Integer })
Arguments:
Expand All @@ -35,6 +39,26 @@ Example of value: `65535` (size = 2) / `16777215` (size = 3)
### **varint** ( )
Arguments: None

[Protobuf](https://developers.google.com/protocol-buffers/docs/encoding#varints)-compatible representation for variable-length integers using one or more bytes.
[Protobuf](https://developers.google.com/protocol-buffers/docs/encoding#varints)-compatible representation for variable-length integers using one or more bytes. Intended for 32-bit unsigned integers, or signed 32-bit integers that have been directly cast to an integer (where the MSB is the sign bit) before encoding.

Example of value: `300` (size is 2 bytes)

### **varint64** ()
Arguments: None

Same as **varint**, but for 64-bit unsigned integers, or signed 64-bit integers that have been directly cast to an integer (where the MSB is the sign bit) before encoding.

### **varint128** ()
Arguments: None

Same as **varint**, but for 128-bit unsigned integers, or signed 128-bit integers that have been directly cast to an integer (where the MSB is the sign bit) before encoding.

### **zigzag32** ()
Arguments: None

Similar to **varint**, except using [ZigZag encoding](https://protobuf.dev/programming-guides/encoding/#signed-ints) for signed integers. Intended for 32-bit signed numbers.

### **zigzag64** ()
Arguments: None

Same as **zigzag32**, but for 64-bit signed integers in ZigZag encoding.
39 changes: 39 additions & 0 deletions doc/datatypes/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ Maps a byte to a string, 1 to "byte", 2 to "short", 3 to "int", 4 to "long".
```
Example of value: `"int"`

### **bitflags** ([ { type: string, flags: object | array, big?: boolean, shift?: number } ])
Arguments:
* type : The underlying integer type (eg varint, lu32).
* flags : Either an array of flag values from LSB to MSB, or an object containing a mappng of valueName => bitMask.
* big : 64+ bits. In langauges like javascript (where all numbers are 64-bit floating points), special data types may have to be used for integers greater than 32 bits, so this must be set to true if the `type` is using the special data type.
* shift : Specify if flags is an object and holds bit positions as values opposed to a bitmask.

Represents boolean flags packed into an integer. Similar to bitfields, but only intended for enumerated boolean flags (each flag occupies 1 bit), and supports arbitrary underlying integer types.

Example:

```json
[
"bitflags",
{
"type": "lu32",
"flags": ["onGround", "inAir"]
}
]
```

or
```yaml
[
"bitflags",
{
"type": "lu32",
"big": true,
"flags": {
"onGround": 0b1,
"inAir": 0b10
}
}
]
```

Example of value to pass when writing: `{"flags": { "onGround": true, "inAir": false } }`. Likewise when reading you will get a similar object back, with a extra `_value` field holding the raw integer.


### **pstring** ({ countType: Type, ?count: Countable })
Arguments:
* countType : the type of the length prefix
Expand Down
5 changes: 5 additions & 0 deletions schemas/datatype.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
{ "$ref": "lu64" },
{ "$ref": "int" },
{ "$ref": "varint" },
{ "$ref": "varint64" },
{ "$ref": "varint128" },
{ "$ref": "zigzag32" },
{ "$ref": "zigzag64" },
{ "$ref": "lint" },

{ "$ref": "array" },
Expand All @@ -40,6 +44,7 @@
{ "$ref": "pstring" },
{ "$ref": "buffer" },
{ "$ref": "bitfield" },
{ "$ref": "bitflags" },
{ "$ref": "mapper" }
]
}
12 changes: 12 additions & 0 deletions schemas/numeric.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
"varint": {
"enum": ["varint"]
},
"varint64": {
"enum": ["varint64"]
},
"varint128": {
"enum": ["varint128"]
},
"zigzag32": {
"enum": ["zigzag32"]
},
"zigzag64": {
"enum": ["zigzag64"]
},
"int": {
"title": "int",
"type": "array",
Expand Down
14 changes: 14 additions & 0 deletions schemas/utils.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@
],
"additionalItems": false
},
"bitflags": {
"title": "bitflags",
"type": "array",
"items": [
{
"enum": ["bitflags"]
},
{
"type": "object",
"additionalItems": false
}
],
"additionalItems": false
},
"mapper": {
"title": "mapper",
"type": "array",
Expand Down
137 changes: 135 additions & 2 deletions test/utils.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,97 @@
"buffer":["0xff", "0xff", "0xff", "0xff", "0x0f"]
},
{
"description":"maximum varint",
"description":"maximum varint (32bit)",
"value":2147483647,
"buffer":["0xff", "0xff", "0xff", "0xff", "0x07"]
},
{
"description":"minimum varint",
"description":"minimum varint (32bit)",
"value":-2147483648,
"buffer":["0x80", "0x80", "0x80", "0x80", "0x08"]
}
]
},
{
"type":"varint64",
"values":[
{
"description":"8-bit integer",
"value":1,
"buffer":["0x01"]
},
{
"description":"8-bit maximum integer",
"value":127,
"buffer":["0x7f"]
},
{
"description":"16-bit integer",
"value":300,
"buffer":["0xac", "0x02"]
},
{
"description":"24-bit integer",
"value":100000,
"buffer":["0xa0", "0x8d", "0x06"]
},
{
"description":"32-bit integer",
"value":16909060,
"buffer":["0x84", "0x86", "0x88", "0x08"]
}
]
},
{
"type":"varint128",
"values":[
{
"description":"8-bit integer",
"value":1,
"buffer":["0x01"]
},
{
"description":"8-bit maximum integer",
"value":127,
"buffer":["0x7f"]
},
{
"description":"16-bit integer",
"value":300,
"buffer":["0xac", "0x02"]
},
{
"description":"24-bit integer",
"value":100000,
"buffer":["0xa0", "0x8d", "0x06"]
},
{
"description":"32-bit integer",
"value":16909060,
"buffer":["0x84", "0x86", "0x88", "0x08"]
}
]
},
{
"type":"zigzag32",
"values":[
{
"description":"8-bit integer",
"value":1,
"buffer":["0x02"]
}
]
},
{
"type":"zigzag64",
"values":[
{
"description":"8-bit integer",
"value":1,
"buffer":["0x02"]
}
]
},
{
"type":"buffer",
"subtypes":[
Expand Down Expand Up @@ -338,6 +418,59 @@
}
]
},
{
"type":"bitflags",
"subtypes":[
{
"description":"8bit bitset flag array",
"type":[
"bitflags",
{ "type": "u8", "flags": ["onGround"] }
],
"values":[
{
"value":{
"_value": 1,
"onGround": true
},
"buffer":["0x01"]
}
]
},
{
"description":"8bit bitset flag object",
"type":[
"bitflags",
{ "type": "u8", "flags": { "onGround": 1 } }
],
"values":[
{
"value":{
"_value": 1,
"onGround": true
},
"buffer":["0x01"]
}
]
},
{
"description":"8bit bitset flag big object",
"type":[
"bitflags",
{ "type": "u8", "big": true, "flags": { "onGround": 1 } }
],
"values":[
{
"value":{
"_value": 1,
"onGround": true
},
"buffer":["0x01"]
}
]
}
]
},
{
"type":"mapper",
"subtypes":[
Expand Down