|
3 | 3 | Created: Jan 19 2022 |
4 | 4 | Purpose: C-Based MessagePack for SWI-Prolog |
5 | 5 |
|
6 | | -Copyright (c) 2022, Roy Ratcliffe, Northumberland, United Kingdom |
| 6 | +Copyright (c) 2022, 2025, Roy Ratcliffe, Northumberland, United Kingdom |
7 | 7 |
|
8 | 8 | Permission is hereby granted, free of charge, to any person obtaining a |
9 | 9 | copy of this software and associated documentation files (the |
|
30 | 30 | [ msgpack//1, % ?Term |
31 | 31 |
|
32 | 32 | msgpack_object//1, % ?Object |
| 33 | + msgpack_key//1, % ?Key |
33 | 34 | msgpack_objects//1, % ?Objects |
34 | 35 |
|
35 | 36 | msgpack_nil//0, |
|
60 | 61 |
|
61 | 62 | % map format family |
62 | 63 | msgpack_map//2, % :OnPair,?Map |
| 64 | + msgpack_map//1, % ?Map |
| 65 | + msgpack_pair//3, % :OnKey,:OnValue,KeyValuePair |
63 | 66 |
|
64 | 67 | % ext format family |
65 | 68 | msgpack_ext//1, % ?Term |
66 | 69 | msgpack_ext//2 % ?Type,?Ext |
67 | 70 | ]). |
68 | | -:- autoload(library(dcg/high_order), [sequence//2, sequence/4]). |
| 71 | +:- autoload(library(dcg/high_order), [sequence//2]). |
69 | 72 | :- autoload(library(utf8), [utf8_codes/3]). |
70 | 73 |
|
71 | 74 | :- use_foreign_library(foreign(msgpackc)). |
|
120 | 123 | msgpack(str(Str)) --> msgpack_str(Str), !. |
121 | 124 | msgpack(bin(Bin)) --> msgpack_bin(Bin), !. |
122 | 125 | msgpack(array(Array)) --> msgpack_array(msgpack, Array), !. |
123 | | -msgpack(map(Map)) --> msgpack_map(msgpack_pair(msgpack, msgpack), Map), !. |
| 126 | +msgpack(map(Map)) --> msgpack_map(Map), !. |
124 | 127 | msgpack(Term) --> msgpack_ext(Term). |
125 | 128 |
|
126 | 129 | %! msgpack_object(?Object)// is semidet. |
|
256 | 259 | % alternative representation for many integers. |
257 | 260 |
|
258 | 261 | msgpack_float(Float) --> |
259 | | - { float64(Float, Bytes, []), |
260 | | - Bytes \= [_, _, _, _, 0, 0, 0, 0] |
261 | | - }, |
262 | | - !, |
263 | | - [0xcb|Bytes]. |
264 | | -msgpack_float(Float) --> msgpack_float(_, Float), !. |
| 262 | + { float64(Float, Bytes, []), |
| 263 | + Bytes \= [_, _, _, _, 0, 0, 0, 0] |
| 264 | + }, |
| 265 | + !, |
| 266 | + [0xcb], |
| 267 | + Bytes. |
| 268 | +msgpack_float(Float) --> msgpack_float(_, Float). |
265 | 269 |
|
266 | 270 | msgpack_float(32, Float) --> [0xca], float32(Float). |
267 | 271 | msgpack_float(64, Float) --> [0xcb], float64(Float). |
|
391 | 395 | % Unifies Str with the shortest packed UTF-8 string message. |
392 | 396 |
|
393 | 397 | msgpack_str(Str) --> msgpack_fixstr(Str), !. |
394 | | -msgpack_str(Str) --> msgpack_str(_, Str), !. |
| 398 | +msgpack_str(Str) --> msgpack_str(_, Str). |
395 | 399 |
|
396 | 400 | %! msgpack_fixstr(?Str)// is semidet. |
397 | 401 | % |
|
493 | 497 | % if 32 bits is not enough to unify the number of bytes because the |
494 | 498 | % byte-list has more than four thousand megabytes. |
495 | 499 |
|
496 | | -msgpack_bin(Bytes) --> msgpack_bin(_, Bytes), !. |
| 500 | +msgpack_bin(Bytes) --> msgpack_bin(_, Bytes). |
497 | 501 |
|
498 | 502 | %! msgpack_bin(?Width, ?Bytes:list)// is nondet. |
499 | 503 | % |
|
547 | 551 | % predicate. |
548 | 552 |
|
549 | 553 | msgpack_array(OnElement, Array) --> msgpack_fixarray(OnElement, Array), !. |
550 | | -msgpack_array(OnElement, Array) --> msgpack_array(OnElement, _, Array), !. |
| 554 | +msgpack_array(OnElement, Array) --> msgpack_array(OnElement, _, Array). |
551 | 555 |
|
552 | 556 | %! msgpack_fixarray(:OnElement, Array)// is semidet. |
553 | 557 | %! msgpack_array(:OnElement, ?Width, ?Array)// is nondet. |
|
619 | 623 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
620 | 624 |
|
621 | 625 | %! msgpack_map(:OnPair, ?Map:list)// is semidet. |
| 626 | +%! msgpack_map(?Map:list)// is semidet. |
622 | 627 | % |
623 | 628 | % Unify with Map using OnPair as the pair-wise grammar. |
624 | 629 |
|
625 | 630 | msgpack_map(OnPair, Map) --> msgpack_fixmap(OnPair, Map), !. |
626 | | -msgpack_map(OnPair, Map) --> msgpack_map(OnPair, _, Map), !. |
| 631 | +msgpack_map(OnPair, Map) --> msgpack_map(OnPair, _, Map). |
| 632 | + |
| 633 | +msgpack_map(Map) --> msgpack_map(msgpack_pair(msgpack, msgpack), Map). |
627 | 634 |
|
628 | 635 | msgpack_fixmap(OnPair, Map) --> |
629 | 636 | { var(Map), |
|
667 | 674 | map_width_format(16, 0xde). |
668 | 675 | map_width_format(32, 0xdf). |
669 | 676 |
|
| 677 | +%! msgpack_pair(:OnKey, :OnValue, KeyValuePair)// is semidet. |
| 678 | + |
670 | 679 | msgpack_pair(OnKey, OnValue, Key-Value) --> |
671 | 680 | call(OnKey, Key), |
672 | 681 | call(OnValue, Value). |
|
679 | 688 | { dict_create(Dict, _, Pairs) |
680 | 689 | }. |
681 | 690 | msgpack_dict(OnPair, Dict) --> |
682 | | - { dict_pairs(Dict, _, Pairs) |
| 691 | + { is_dict(Dict), |
| 692 | + dict_pairs(Dict, _, Pairs) |
683 | 693 | }, |
684 | 694 | msgpack_map(OnPair, Pairs). |
685 | 695 |
|
|
712 | 722 | % Type is a signed integer. Ext is a list of byte codes. |
713 | 723 |
|
714 | 724 | msgpack_ext(Type, Ext) --> msgpack_fixext(Type, Ext), !. |
715 | | -msgpack_ext(Type, Ext) --> msgpack_ext(_, Type, Ext), !. |
| 725 | +msgpack_ext(Type, Ext) --> msgpack_ext(_, Type, Ext). |
716 | 726 |
|
717 | 727 | msgpack_fixext(Type, Ext) --> |
718 | 728 | { var(Type), |
|
0 commit comments