PreviousUpNext

15.4.548  src/lib/compiler/front/basics/map/picklehash.pkg

## picklehash.pkg

# Compiled by:
#     src/lib/compiler/front/basics/basics.sublib

#   We use pickle hashes to provide portable, abstract,
#   filesystem-independent identifiers for pickles
#   (compiled files).
#


stipulate
    package err =  error_message;                                               # error_message                 is from   src/lib/compiler/front/basics/errormsg/error-message.pkg
    package ns  =  number_string;                                               # number_string                 is from   src/lib/std/src/number-string.pkg
    package str =  string;                                                      # string                        is from   src/lib/std/string.pkg
    package u1b =  one_byte_unt;                                                # one_byte_unt                  is from   src/lib/std/one-byte-unt.pkg
    package v1b =  vector_of_one_byte_unts;                                     # vector_of_one_byte_unts       is from   src/lib/std/src/vector-of-one-byte-unts.pkg
herein

    package   picklehash
    :         Picklehash                                                        # Picklehash                    is from   src/lib/compiler/front/basics/map/picklehash.api
    {
        Picklehash = PICKLEHASH  v1b::Vector;

        pickle_hash_size = 16;

        fun compare (PICKLEHASH v1, PICKLEHASH v2)
            =
            str::compare
              (
                byte::bytes_to_string v1,
                byte::bytes_to_string v2
              );

                                

        fun to_bytes (PICKLEHASH x)
            =
            x;

        fun from_bytes v
            =
            if (v1b::length v == pickle_hash_size)      PICKLEHASH v;
            else                                        err::impossible "picklehash::stringToStamp";
            fi;

        # Convert the Picklehash to a printable representation (hex digits) 

        fun to_hex (PICKLEHASH pid)
            = 
            {   fun cvt_byte b
                    =
                    ns::pad_left '0' 2 (u1b::to_string b);


                fun f (b, l)
                    =
                    cvt_byte b ! l;

                str::cat (v1b::fold_backward f [] pid);
            };

        fun from_hex s
            =
            {
                THE (PICKLEHASH (v1b::from_fn (pickle_hash_size, onebyte)))
                where
                    fun onebyte i
                        =
                        {   i2 =   2 * i;
                            c1 =   str::get_byte_as_char (s, i2);
                            c2 =   str::get_byte_as_char (s, i2 + 1);

                            the (u1b::from_string (implode [c1, c2]));
                        };
                end;
            }
            except _
                =
                NULL;

    };
end;

## Copyright (c) 2004 by The Fellowship of SML/NJ
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext