2 434
edições
mSem resumo de edição |
|||
Linha 366: | Linha 366: | ||
O "encode" é a conversão "vbit to base-h", o "decode" é a conversão "base-h to vbit", daí os nomes de função <code>vbit_to_baseh()</code> e <code>baseh_to_vbit()</code>, no contexto do [https://git.osm.codes/NaturalCodes/tree/main/src-sql SQL-schema NatCode]. | O "encode" é a conversão "vbit to base-h", o "decode" é a conversão "base-h to vbit", daí os nomes de função <code>vbit_to_baseh()</code> e <code>baseh_to_vbit()</code>, no contexto do [https://git.osm.codes/NaturalCodes/tree/main/src-sql SQL-schema NatCode]. | ||
A seguir algoritmos simplificados. A função de "encode bitstring" converte uma cadeia de bits em texto base2h, base4h, base8h ou base16h. | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
CREATE FUNCTION natcod.vbit_to_baseh( | CREATE FUNCTION natcod.vbit_to_baseh( | ||
p_val varbit, -- input | p_val varbit, -- input | ||
p_base int DEFAULT | p_base int DEFAULT 16 -- 16 for base16h (default), 8 for base8h, 4 for base4h or 2 for base2h. | ||
) RETURNS text AS $f$ | ) RETURNS text AS $f$ | ||
DECLARE | DECLARE | ||
Linha 385: | Linha 384: | ||
trtypes JSONb := '{"2":[1,1], "4":[1,2], "8":[2,3], "16":[3,4]}'::JSONb; -- TrPos,bits. Can optimize? by sparse array. | trtypes JSONb := '{"2":[1,1], "4":[1,2], "8":[2,3], "16":[3,4]}'::JSONb; -- TrPos,bits. Can optimize? by sparse array. | ||
trpos int; | trpos int; | ||
baseh "char"[] := array[ -- | baseh "char"[] := array[ -- the standards for Baseh: | ||
'[0:15]={G,Q,x,x,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --1. 1 bit in 4h,8h,16h | '[0:15]={G,Q,x,x,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --1. 1 bit in 4h,8h,16h | ||
'[0:15]={0,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --2. 2 bits in 4h | '[0:15]={0,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}'::"char"[], --2. 2 bits in 4h | ||
Linha 403: | Linha 391: | ||
'[0:15]={J,K,N,P,S,T,Z,Y,x,x,x,x,x,x,x,x}'::"char"[], --5. 3 bits in 16h | '[0:15]={J,K,N,P,S,T,Z,Y,x,x,x,x,x,x,x,x}'::"char"[], --5. 3 bits in 16h | ||
'[0:15]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}'::"char"[] --6. 4 bits in 16h | '[0:15]={0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}'::"char"[] --6. 4 bits in 16h | ||
]; | -- Hexadecimals by standard alphabet of https://tools.ietf.org/html/rfc4648#section-6 | ||
]; -- jumpping I,O and L,U,W,X letters. | |||
BEGIN | |||
vlen := bit_length(p_val); | vlen := bit_length(p_val); | ||
tr_selected := trtypes->(p_base::text); -- can be array instead of JSON | tr_selected := trtypes->(p_base::text); -- can be array instead of JSON | ||
Linha 429: | Linha 418: | ||
RETURN ret; | RETURN ret; | ||
END | END | ||
$f$ LANGUAGE | $f$ LANGUAGE PLpgSQL IMMUTABLE; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
edições