Skip to content

Commit b077c63

Browse files
authored
Merge pull request #10 from royratcliffe/minor-fixes
Minor fixes
2 parents e20fc6f + 390c530 commit b077c63

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Uses [Semantic Versioning](https://semver.org/). Always [keep a change
44
log](https://keepachangelog.com/en/1.0.0/).
55

6+
## [0.2.2] - 2025-04-21
7+
### Fixed
8+
- The predicate `msgpack_dict` will fail if the argument provided is not a
9+
dictionary.
10+
- Latest SWI Prolog does not like `[0xcb|Bytes]`.
11+
612
## [0.2.1] - 2022-05-21
713
### Changed
814
- Comment out misleading fail coverage

pack.pl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name(msgpackc).
2-
version('0.2.1').
2+
version('0.2.2').
33
title('C-Based MessagePack for SWI-Prolog').
4-
author('Roy Ratcliffe', 'royratcliffe@me.com').
5-
packager('Roy Ratcliffe', 'royratcliffe@me.com').
6-
maintainer('Roy Ratcliffe', 'royratcliffe@me.com').
4+
author('Roy Ratcliffe', 'roy@ratcliffe.me').
5+
packager('Roy Ratcliffe', 'roy@ratcliffe.me').
6+
maintainer('Roy Ratcliffe', 'roy@ratcliffe.me').
77
home('https://github.com/swipl/msgpackc').
88
download('https://github.com/swipl/msgpackc/releases/*.zip').

prolog/msgpackc.pl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Created: Jan 19 2022
44
Purpose: C-Based MessagePack for SWI-Prolog
55
6-
Copyright (c) 2022, Roy Ratcliffe, Northumberland, United Kingdom
6+
Copyright (c) 2022, 2025, Roy Ratcliffe, Northumberland, United Kingdom
77
88
Permission is hereby granted, free of charge, to any person obtaining a
99
copy of this software and associated documentation files (the
@@ -30,6 +30,7 @@
3030
[ msgpack//1, % ?Term
3131

3232
msgpack_object//1, % ?Object
33+
msgpack_key//1, % ?Key
3334
msgpack_objects//1, % ?Objects
3435

3536
msgpack_nil//0,
@@ -60,12 +61,14 @@
6061

6162
% map format family
6263
msgpack_map//2, % :OnPair,?Map
64+
msgpack_map//1, % ?Map
65+
msgpack_pair//3, % :OnKey,:OnValue,KeyValuePair
6366

6467
% ext format family
6568
msgpack_ext//1, % ?Term
6669
msgpack_ext//2 % ?Type,?Ext
6770
]).
68-
:- autoload(library(dcg/high_order), [sequence//2, sequence/4]).
71+
:- autoload(library(dcg/high_order), [sequence//2]).
6972
:- autoload(library(utf8), [utf8_codes/3]).
7073

7174
:- use_foreign_library(foreign(msgpackc)).
@@ -120,7 +123,7 @@
120123
msgpack(str(Str)) --> msgpack_str(Str), !.
121124
msgpack(bin(Bin)) --> msgpack_bin(Bin), !.
122125
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), !.
124127
msgpack(Term) --> msgpack_ext(Term).
125128

126129
%! msgpack_object(?Object)// is semidet.
@@ -256,12 +259,13 @@
256259
% alternative representation for many integers.
257260

258261
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).
265269

266270
msgpack_float(32, Float) --> [0xca], float32(Float).
267271
msgpack_float(64, Float) --> [0xcb], float64(Float).
@@ -391,7 +395,7 @@
391395
% Unifies Str with the shortest packed UTF-8 string message.
392396

393397
msgpack_str(Str) --> msgpack_fixstr(Str), !.
394-
msgpack_str(Str) --> msgpack_str(_, Str), !.
398+
msgpack_str(Str) --> msgpack_str(_, Str).
395399

396400
%! msgpack_fixstr(?Str)// is semidet.
397401
%
@@ -493,7 +497,7 @@
493497
% if 32 bits is not enough to unify the number of bytes because the
494498
% byte-list has more than four thousand megabytes.
495499

496-
msgpack_bin(Bytes) --> msgpack_bin(_, Bytes), !.
500+
msgpack_bin(Bytes) --> msgpack_bin(_, Bytes).
497501

498502
%! msgpack_bin(?Width, ?Bytes:list)// is nondet.
499503
%
@@ -547,7 +551,7 @@
547551
% predicate.
548552

549553
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).
551555

552556
%! msgpack_fixarray(:OnElement, Array)// is semidet.
553557
%! msgpack_array(:OnElement, ?Width, ?Array)// is nondet.
@@ -619,11 +623,14 @@
619623
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
620624

621625
%! msgpack_map(:OnPair, ?Map:list)// is semidet.
626+
%! msgpack_map(?Map:list)// is semidet.
622627
%
623628
% Unify with Map using OnPair as the pair-wise grammar.
624629

625630
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).
627634

628635
msgpack_fixmap(OnPair, Map) -->
629636
{ var(Map),
@@ -667,6 +674,8 @@
667674
map_width_format(16, 0xde).
668675
map_width_format(32, 0xdf).
669676

677+
%! msgpack_pair(:OnKey, :OnValue, KeyValuePair)// is semidet.
678+
670679
msgpack_pair(OnKey, OnValue, Key-Value) -->
671680
call(OnKey, Key),
672681
call(OnValue, Value).
@@ -679,7 +688,8 @@
679688
{ dict_create(Dict, _, Pairs)
680689
}.
681690
msgpack_dict(OnPair, Dict) -->
682-
{ dict_pairs(Dict, _, Pairs)
691+
{ is_dict(Dict),
692+
dict_pairs(Dict, _, Pairs)
683693
},
684694
msgpack_map(OnPair, Pairs).
685695

@@ -712,7 +722,7 @@
712722
% Type is a signed integer. Ext is a list of byte codes.
713723

714724
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).
716726

717727
msgpack_fixext(Type, Ext) -->
718728
{ var(Type),

prolog/msgpackc.plt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ test(msgpack_str,
9999
])) :-
100100
phrase(msgpackc:msgpack_str(A, "hello"), B).
101101

102+
test(msgpack_array, fail) :-
103+
phrase(msgpack_array(msgpack, _{}), _).
104+
105+
test(msgpack_map, A == [128]) :-
106+
phrase(msgpack_map([]), A).
107+
102108
test(msgpack_bin, true(A == [0xc4, 0])) :-
103109
phrase(msgpack_bin(8, []), A).
104110
test(msgpack_bin, true(A == [0xc4, 3, 1, 2, 3])) :-

0 commit comments

Comments
 (0)