PreviousUpNext

15.4.1076  src/lib/std/src/byte.pkg

## byte.pkg

# Compiled by:
#     src/lib/std/src/standard-core.sublib

###               "Cooking is like love. It should be
###                entered into with abandon or not at all."
###
###                           -- Harriet Van Horne


stipulate
    package w8  =  one_byte_unt_guts;                   # one_byte_unt_guts                     is from   src/lib/std/src/one-byte-unt-guts.pkg
    package ns  =  number_string;                       # number_string                         is from   src/lib/std/src/number-string.pkg
    package s1u =     vector_slice_of_one_byte_unts;    #    vector_slice_of_one_byte_unts      is from   src/lib/std/src/vector-slice-of-one-byte-unts.pkg
    package t1u =  rw_vector_slice_of_one_byte_unts;    # rw_vector_slice_of_one_byte_unts      is from   src/lib/std/src/rw-vector-slice-of-one-byte-unts.pkg
    package v1u =     vector_of_one_byte_unts;          #    vector_of_one_byte_unts            is from   src/lib/std/src/vector-of-one-byte-unts.pkg
#   package w1u =  rw_vector_of_one_byte_unts;          # rw_vector_of_one_byte_unts            is from   src/lib/std/src/rw-vector-of-one-byte-unts.pkg
    package str =  string_guts;                         # string_guts                           is from   src/lib/std/src/string-guts.pkg
    package int =  int_guts;                            # int_guts                              is from   src/lib/std/src/int-guts.pkg
herein

    package   byte
    : (weak)  Byte                                      # Byte          is from   src/lib/std/src/byte.api
    {                                                   # inline_t      is from   src/lib/core/init/built-in.pkg

        byte_to_char    =   inline_t::cast:  one_byte_unt::Unt -> Char;
        char_to_byte    =   inline_t::cast:  Char              -> one_byte_unt::Unt;

        bytes_to_string =   inline_t::cast:  v1u::Vector -> String;
        string_to_bytes =   inline_t::cast:  String      -> v1u::Vector;

        unpack_string_vector =  bytes_to_string o s1u::to_vector;
        unpack_string        =  bytes_to_string o t1u::to_vector;

        stipulate

            package wu =  inline_t::rw_vector_of_one_byte_unts; # inline_t      is from   src/lib/core/init/built-in.pkg
            package vc =  inline_t::vector_of_chars;
            package ss =  substring;

            Substring' = SS  ((String, Int, Int));

            # The substring type is abstract,
            # so we use a cast to an equivalent type
            # to get around this problem.                               # =8-0   Cursed by ye C gods!!   XXX SUCKO FIXME

            to_ss =   inline_t::cast:  ss::Substring -> Substring';


        herein

            fun pack_string (rw_vector_of_one_byte_unts: wu::Rw_Vector, i, substring: ss::Substring):   Void
                =
                {   (to_ss  substring) ->   SS (src, src_start, src_len);
                    #
                    dst_len =  wu::length  rw_vector_of_one_byte_unts;

                    if  (i  <  0
                    or   i  >  dst_len - src_len
                    )
                         raise exception INDEX_OUT_OF_BOUNDS;
                    fi;

                    cpy (src_start, i, src_len)
                    where
                        fun cpy (_, _, 0) =>   ();
                            #
                            cpy (src_index, dst_index, n)
                                =>
                                {   wu::set (rw_vector_of_one_byte_unts, dst_index, inline_t::cast (vc::get_byte_as_char (src, src_index)));
                                    #
                                    cpy (src_index+1, dst_index+1, n - 1);
                                };
                        end;
                    end;
                };
        end;

         stipulate
            reverse_table =  string_to_bytes  "\x00\x80\x40\xc0\x20\xa0\x60\xe0\
                                              \\x10\x90\x50\xd0\x30\xb0\x70\xf0\
                                              \\x08\x88\x48\xc8\x28\xa8\x68\xe8\
                                              \\x18\x98\x58\xd8\x38\xb8\x78\xf8\
                                              \\x04\x84\x44\xc4\x24\xa4\x64\xe4\
                                              \\x14\x94\x54\xd4\x34\xb4\x74\xf4\
                                              \\x0c\x8c\x4c\xcc\x2c\xac\x6c\xec\
                                              \\x1c\x9c\x5c\xdc\x3c\xbc\x7c\xfc\
                                              \\x02\x82\x42\xc2\x22\xa2\x62\xe2\
                                              \\x12\x92\x52\xd2\x32\xb2\x72\xf2\
                                              \\x0a\x8a\x4a\xca\x2a\xaa\x6a\xea\
                                              \\x1a\x9a\x5a\xda\x3a\xba\x7a\xfa\
                                              \\x06\x86\x46\xc6\x26\xa6\x66\xe6\
                                              \\x16\x96\x56\xd6\x36\xb6\x76\xf6\
                                              \\x0e\x8e\x4e\xce\x2e\xae\x6e\xee\
                                              \\x1e\x9e\x5e\xde\x3e\xbe\x7e\xfe\
                                              \\x01\x81\x41\xc1\x21\xa1\x61\xe1\
                                              \\x11\x91\x51\xd1\x31\xb1\x71\xf1\
                                              \\x09\x89\x49\xc9\x29\xa9\x69\xe9\
                                              \\x19\x99\x59\xd9\x39\xb9\x79\xf9\
                                              \\x05\x85\x45\xc5\x25\xa5\x65\xe5\
                                              \\x15\x95\x55\xd5\x35\xb5\x75\xf5\
                                              \\x0d\x8d\x4d\xcd\x2d\xad\x6d\xed\
                                              \\x1d\x9d\x5d\xdd\x3d\xbd\x7d\xfd\
                                              \\x03\x83\x43\xc3\x23\xa3\x63\xe3\
                                              \\x13\x93\x53\xd3\x33\xb3\x73\xf3\
                                              \\x0b\x8b\x4b\xcb\x2b\xab\x6b\xeb\
                                              \\x1b\x9b\x5b\xdb\x3b\xbb\x7b\xfb\
                                              \\x07\x87\x47\xc7\x27\xa7\x67\xe7\
                                              \\x17\x97\x57\xd7\x37\xb7\x77\xf7\
                                              \\x0f\x8f\x4f\xcf\x2f\xaf\x6f\xef\
                                              \\x1f\x9f\x5f\xdf\x3f\xbf\x7f\xff"
                             ;
        herein
            fun reverse_byte_bits b                                                                                             # Reverse the bit-order of a byte 
                =
                v1u::get (reverse_table, w8::to_int b);
        end;


        fun string_to_hex s                                                                                                     # Convert "abc" -> "61.62.63." etc.
            =
            str::translate
                (\\ c =  ns::pad_left '0' 2 (int::format ns::HEX (char::to_int c)) + ".")
                 s;

        fun bytes_to_hex  bytes                                                                                                 # As above, starting with byte-vector.
            =
            string_to_hex (unpack_string_vector(s1u::make_slice (bytes, 0, NULL)));

        fun string_to_ascii s                                                                                                   # Show printing chars verbatim, everything else as '.', per hexdump tradition.
            =
            str::translate
                (\\ c =  char::is_print c  ??  str::from_char c  ::  ".")
                s;

        fun bytes_to_ascii  bytes                                                                                               # As above, starting with byte-vector.
            =
            string_to_ascii (unpack_string_vector(s1u::make_slice (bytes, 0, NULL)));
    };
end;


## COPYRIGHT (c) 1995 AT&T Bell Laboratories.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext