miaou — le blogv1.0.3

hex4.miaou

← Le code · src/json/hex4.miaou · 26 lignes

(node hex4
  (params (t (Liste Octet)))
  (returns Nat)
  (intent "la valeur des quatre chiffres hexadécimaux en tête de t (d'un échappement \\uXXXX) ; 65536 si t n'en commence pas par quatre")
  (origin "duel 5 : un lecteur JSON en miaou")
  (effects)
  (uses valeur_hex tete sans_tete)
  (requires
    true)
  (ensures
    (le r 65536))
  (examples
    (ex ("00e9x") 233)
    (ex ("D83D") 55357)
    (ex ("12g4") 65536)
    (ex ("12") 65536))
  (equivalence exact)
  (body.high
    (let ((a (tete t)) (b (tete (sans_tete t))) (c (tete (sans_tete (sans_tete t)))) (d (tete (sans_tete (sans_tete (sans_tete t))))))
      (if (or (or (eq a 256) (eq b 256)) (or (eq c 256) (eq d 256)))
        65536
        (let ((va (valeur_hex (to_octet a))) (vb (valeur_hex (to_octet b))) (vc (valeur_hex (to_octet c))) (vd (valeur_hex (to_octet d))))
          (if (or (or (eq va 16) (eq vb 16)) (or (eq vc 16) (eq vd 16)))
            65536
            (add (add (mul va 4096) (mul vb 256)) (add (mul vc 16) vd)))))))
  (end node hex4))