generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(logic): harmonize term to/from bytes functions
- Loading branch information
Showing
14 changed files
with
637 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package predicate | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
|
||
"github.com/okp4/okp4d/x/logic/util" | ||
) | ||
|
||
// HexBytes is a predicate that unifies hexadecimal encoded bytes to a list of bytes. | ||
// | ||
// The signature is as follows: | ||
// | ||
// hex_bytes(?Hex, ?Bytes) is det | ||
// | ||
// Where: | ||
// - Hex is an Atom, string or list of characters in hexadecimal encoding. | ||
// - Bytes is the list of numbers between 0 and 255 that represent the sequence of bytes. | ||
// | ||
// Examples: | ||
// | ||
// # Convert hexadecimal atom to list of bytes. | ||
// - hex_bytes('2c26b46b68ffc68ff99b453c1d3041341342d706483bfa0f98a5e886266e7ae', Bytes). | ||
func HexBytes(vm *engine.VM, hexa, bts engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
return engine.Delay(func(ctx context.Context) *engine.Promise { | ||
var result []byte | ||
|
||
switch h := env.Resolve(hexa).(type) { | ||
case engine.Variable: | ||
case engine.Atom: | ||
src := []byte(h.String()) | ||
result = make([]byte, hex.DecodedLen(len(src))) | ||
_, err := hex.Decode(result, src) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("hex_bytes/2: failed decode hexadecimal %w", err)) | ||
} | ||
default: | ||
return engine.Error(fmt.Errorf("hex_bytes/2: invalid hex type: %T, should be Atom or Variable", h)) | ||
} | ||
|
||
switch b := env.Resolve(bts).(type) { | ||
case engine.Variable: | ||
if result == nil { | ||
return engine.Error(fmt.Errorf("hex_bytes/2: nil hexadecimal conversion in input")) | ||
} | ||
return engine.Unify(vm, bts, util.BytesToStringTermDefault(result), cont, env) | ||
case engine.Compound: | ||
src, err := util.StringTermToBytes(b, "", env) | ||
if err != nil { | ||
return engine.Error(fmt.Errorf("hex_bytes/2: %w", err)) | ||
} | ||
dst := hex.EncodeToString(src) | ||
return engine.Unify(vm, hexa, util.StringToTerm(dst), cont, env) | ||
default: | ||
return engine.Error(fmt.Errorf("hex_bytes/2: invalid hex type: %T, should be Variable or List", b)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.