Skip to content

Instantly share code, notes, and snippets.

@lepinekong
Last active January 4, 2018 12:45
Show Gist options
  • Save lepinekong/4fd51a2897b81f81d1257d5f694bdc7a to your computer and use it in GitHub Desktop.
Save lepinekong/4fd51a2897b81f81d1257d5f694bdc7a to your computer and use it in GitHub Desktop.
base58 Hexadecimal Euclidian Division
Red [
Title: "For debug"
]
__DEBUG_LINE_COUNTER__: 0
??: func [
"Prints a word and the value it refers to (molded)"
'value [word! path! string!]
/start
][
; initialization
if start [
system/words/__DEBUG_LINE_COUNTER__: 0
]
; cum
system/words/__DEBUG_LINE_COUNTER__: system/words/__DEBUG_LINE_COUNTER__ + 1
if (not value? '__TRACING__) [__TRACING__: false]
if not (__TRACING__ = false) [
either (type? :value) = string! [
print rejoin ["[" system/words/__DEBUG_LINE_COUNTER__ "]" " - " :value]
][
prin rejoin ["[" system/words/__DEBUG_LINE_COUNTER__ "]" " - "]
prin mold :value
prin ": "
print either value? :value [
mold get/any :value
] ["unset!"]
]
]
]
Red [
Title: "base58 library"
]
remove-leading-zeros: func[string /hex /local x][
parse string [some #"0" copy x to end]
if hex [
if (mod (length? x) 2) > 0 [
x: rejoin ["0" x]
]
]
return x
]
to-string: func[value /hex /local temp][
either binary? value [
parse mold value [thru "#{" copy value to "}"]
value
;value: remove-leading-zeros/hex value
][
either hex [
remove-leading-zeros/hex to-string to-binary :value
][
to string! :value
]
]
]
last: func ["Returns the last value in a series" s [series!] /n length][
either n [
copy skip head s ((length? s) - length)
][
pick back tail s 1
]
]
if NOT value? 'sys-to-hex [
sys-to-hex: :to-hex
to-hex: func[value [integer! string!] /hex /local temp][
if ((type? value) = integer!) [
temp: to-string/hex value
return debase/base temp 16
;return sys-to-hex value
]
if (type? value) = string! [
; the line below generates bug
;value: remove-leading-zeros/hex value
temp: debase/base value 16
either none? temp [
return to-binary value;
][
either hex [
?? value
return to-binary value
][
return debase/base value 16
]
]
]
]
]
split-hex: func[hex-string][
return split/segment hex-string "" 6 ; ["48656C" "6C6F"]
]
to-hex2: func[value [integer! string!] /local temp][
if ((type? value) = integer!) [
temp: to-string/hex value
return debase/base temp 16
;return sys-to-hex value
]
if (type? value) = string! [
; the line below generates bug
value: remove-leading-zeros/hex value
return debase/base value 16
]
]
to-integer: func ["Convert to integer! single value or block" value /hex /local out][
either block? value [
out: copy []
foreach element value [
append out to-integer element
]
out
][
if hex [
value: to-hex :value
]
if error? try [
return to integer! :value
][
return to integer! (to-hex :value)
]
]
]
split: func[ {Break a string series into pieces using the provided delimiters}
series [any-string!] dlm [string! char! bitset!] /segment n /local s
num][
either segment [
parse series [collect some keep [n skip | thru end]]
][
num: either string? dlm [length? dlm] [1]
parse series [collect any [copy s [to dlm | to end] keep (s) num skip]]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment