Changeset 34441 in project


Ignore:
Timestamp:
08/28/17 22:29:24 (3 months ago)
Author:
kon
Message:

add hex->string

Location:
release/4/string-utils/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • release/4/string-utils/trunk/string-hexadecimal.scm

    r34414 r34441  
    11;;;; string-hexadecimal.scm  -*- Hen -*-
    22;;;; Kon Lovett, Aug '10
     3;;;; Kon Lovett, Aug '17
    34
    45(module string-hexadecimal
    56
    67(;export
    7   string->hex *string->hex)
     8  string->hex *string->hex
     9  hex->string *hex->string)
    810
    911(import scheme)
     
    1113(import chicken)
    1214
    13 (import (only lolevel number-of-bytes))
     15(import
     16  (only lolevel number-of-bytes))
    1417(require-library lolevel)
    1518
    1619(import
    17   (only to-hex str_to_hex)
     20  (only to-hex str_to_hex hex_to_str)
    1821  (only type-checks check-natural-fixnum check-string))
    1922(require-library to-hex type-checks)
    2023
    21 #|
    22 (import
    23   (only (srfi 1) drop drop-right)
    24   (only (srfi 13) string-pad string-concatenate))
    25 (require-library
    26   (srfi 1)
    27   (srfi 13))
    28 |#
    29 
    3024(declare
    3125  (bound-to-procedure
    32     ##sys#signal-hook))
     26    ##sys#signal-hook
     27    ##sys#make-string))
     28
     29;;
     30
     31(define-inline (fxzero? x) (fx= 0 x))
    3332
    3433;;
     
    5049    (*string->hex str start end) ) )
    5150
     51(define (hex->string str #!optional (start 0) (end #f))
     52  (let ((len (number-of-bytes (check-string 'hex->string str))))
     53    (unless (fxzero? (fxmod len 2))
     54      (error 'hex->string "too few characters" str) )
     55    (let ((end (or end len)))
     56      (check-subvector-indexes 'hex->string start end)
     57      (*hex->string str start end) ) ) )
     58
     59;;
     60
    5261(define (*string->hex str start end)
    5362  (let ((len (fx- end start)))
    5463    (if (fx= 0 len)
    5564      ""
    56       (str_to_hex (make-string (fx* len 2)) str start len) ) ) )
     65      (str_to_hex (##sys#make-string (fx* len 2)) str start len) ) ) )
    5766
    58 #;
     67(define (*hex->string str start end)
     68  (let ((len (fx- end start)))
     69    (if (fx= 0 len)
     70      ""
     71      (hex_to_str (##sys#make-string (fx/ len 2)) str start len) ) ) )
     72
     73#|
     74(import
     75  (only (srfi 1) drop drop-right)
     76  (only (srfi 13) string-pad string-concatenate))
     77(require-library (srfi 1) (srfi 13))
     78
    5979(define (*string->hex str start end)
    6080  (let* ((ls (string->list str) )
     
    6787          (string-pad (number->string (char->integer c) 16) 2 #\0))
    6888      ls)) ) )
     89|#
     90
     91#|
     92(import (only (srfi 13) reverse-list->string))
     93(require-library (srfi 13))
     94
     95(define (*hex->string str)
     96  (let ((len (string-length str)))
     97    (let loop ((i 0) (ls '()))
     98      (if (fx<= len i)
     99        (reverse-list->string ls)
     100        (let ((ni (fx+ i 2) ))
     101          (loop ni (cons (integer->char (string->number (substring str i ni) 16)) ls)) ) ) ) ) )
     102|#
    69103
    70104) ;module string-hexadecimal
  • release/4/string-utils/trunk/string-utils.setup

    r34414 r34441  
    55(verify-extension-name "string-utils")
    66
    7 (setup-shared-extension-module 'unicode-utils (extension-version "1.2.6")
     7(setup-shared-extension-module 'unicode-utils (extension-version "1.3.0")
    88  #:inline? #t
    99  #:types? #t
     
    1414    -no-procedure-checks))
    1515
    16 (setup-shared-extension-module 'memoized-string (extension-version "1.2.6")
     16(setup-shared-extension-module 'memoized-string (extension-version "1.3.0")
    1717  #:inline? #t
    1818  #:types? #t
     
    2424    -no-procedure-checks))
    2525
    26 (setup-shared-extension-module 'to-hex (extension-version "1.2.6"))
     26(setup-shared-extension-module 'to-hex (extension-version "1.3.0"))
    2727
    28 (setup-shared-extension-module 'string-hexadecimal (extension-version "1.2.6")
     28(setup-shared-extension-module 'string-hexadecimal (extension-version "1.3.0")
    2929  #:inline? #t
    3030  #:types? #t
     
    3535    -no-procedure-checks))
    3636
    37 (setup-shared-extension-module 'string-utils (extension-version "1.2.6")
     37(setup-shared-extension-module 'string-utils (extension-version "1.3.0")
    3838  #:inline? #t
    3939  #:types? #t
  • release/4/string-utils/trunk/tests/run.scm

    r27751 r34441  
    4848)
    4949
     50(test-group "Hex -> String"
     51        (test "abc" (hex->string "616263"))
     52        (test "ABCDEF" (hex->string "414243444546"))
     53        (test "BCDEF" (hex->string "4243444546"))
     54        (test "BC" (hex->string "4243"))
     55        (test "jkL]" (hex->string "6a6B4c5D"))
     56        (test-error (hex->string "424"))
     57)
     58
    5059(test-end)
    5160
  • release/4/string-utils/trunk/to-hex.scm

    r34123 r34441  
    1010  u8vec_to_hex
    1111  blob_to_hex
    12   str_to_hex)
     12  str_to_hex
     13  ;
     14  hex_to_str
     15  hex_to_blob)
    1316
    1417(import scheme)
     
    1619
    1720;;;
     21
     22;;
    1823
    1924#>
     
    2833  }
    2934}
     35
     36static void
     37hex_to_bv( uint8_t *out, uint8_t *in, int off, int len )
     38{
     39# define hex_nibble(c)  (isdigit(c) ? ((c) - '0') : (((c) - 'a') + 10))
     40
     41  static char digits[] = "0123456789abcdef";
     42  in += off;
     43  while( 0 <= (len -= 2) ) {
     44    *out++ = (hex_nibble(tolower(in[0])) << 4) | hex_nibble(tolower(in[1]));
     45    in += 2;
     46  }
     47
     48# undef hex_nibble
     49}
    3050<#
     51
     52;;
    3153
    3254(define C_str_to_hex
     
    4870  (foreign-lambda void "bv_to_hex" nonnull-scheme-pointer nonnull-c-pointer int int))
    4971
     72;;
     73
     74(define C_hex_to_str
     75  (foreign-lambda void "hex_to_bv" nonnull-scheme-pointer nonnull-scheme-pointer int int))
     76
    5077;;;
     78
     79;;
    5180
    5281(define (str_to_hex out in off len)
     
    7099  out )
    71100
     101;;
     102
     103(define (hex_to_str out in off len)
     104  (C_hex_to_str out in off len)
     105  out )
     106
     107(define (hex_to_blob out in off len)
     108  (C_hex_to_str out in off len)
     109  out )
     110
    72111) ;module to-hex
Note: See TracChangeset for help on using the changeset viewer.