comparison modules/bytearray.tp @ 296:2a0a88799737

A number of fixes and enhancmenets to "normal" bytearrays
author Michael Pavone <pavone@retrodev.com>
date Thu, 24 Jul 2014 23:52:37 -0700
parents d1dc2d70bdfd
children 4c669942c30d
comparison
equal deleted inserted replaced
295:9a30510f6e52 296:2a0a88799737
5 normal <- :size { 5 normal <- :size {
6 #{ 6 #{
7 llProperty: bytes withType: uint32_t 7 llProperty: bytes withType: uint32_t
8 llProperty: buffer withType: (void ptr) 8 llProperty: buffer withType: (void ptr)
9 llMessage: _init_buf withVars: { 9 llMessage: _init_buf withVars: {
10 sz <_ obj_int32 ptr 10 sz <- obj_int32 ptr
11 } andCode: :sz { 11 } andCode: :sz {
12 bytes <- sz num 12 bytes <- sz num
13 buffer <- GC_MALLOC_ATOMIC: bytes 13 buffer <- GC_MALLOC_ATOMIC: bytes
14 self 14 self
15 } 15 }
16 16
17 llMessage: _buf_ptr withVArs: { 17 llMessage: _buf_ptr withVars: {
18 ptrret <- cpointer ptr 18 ptrret <- cpointer ptr
19 } andCode: { 19 } andCode: {
20 ptrret <- make_object: (addr_of: cpointer_meta) NULL 0 20 ptrret <- make_object: (addr_of: cpointer_meta) NULL 0
21 ptrret val!: buffer 21 ptrret val!: buffer
22 ptrret 22 ptrret
44 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0 44 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0
45 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num)) 45 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num))
46 ret 46 ret
47 } 47 }
48 48
49 llMessage: stringFrom:to withVars:{ 49 llMessage: shrinkTo withVars: {
50 newsize <- obj_int32 ptr
51 } andCode: :newsize {
52 if: (newsize num) < bytes {
53 bytes <- newsize num
54 }
55 self
56 }
57
58 llMessage: stringFrom:to withVars: {
50 from <- obj_int32 ptr 59 from <- obj_int32 ptr
51 to <- obj_int32 ptr 60 to <- obj_int32 ptr
52 str <- string ptr 61 str <- string ptr
53 } andCode: :from :to { 62 } andCode: :from :to {
54 //probably should do some UTF-8 validation at some point 63 //probably should do some UTF-8 validation at some point
55 str <- make_object: (addr_of: string_meta) NULL 0 64 str <- make_object: (addr_of: string_meta) NULL 0
56 str bytes!: (to num) - (from num) 65 str bytes!: (to num) - (from num)
57 str len!: (str bytes) 66 str len!: (str bytes)
58 str data!: (GC_MALLOC_ATOMIC: (str bytes) + 1) 67 str data!: (GC_MALLOC_ATOMIC: (str bytes) + 1)
59 memcpy: (str data) buffer (str bytes) 68 memcpy: (str data) (buffer castTo: (uint8_t ptr)) + (from num) (str bytes)
60 (str data) set: (str bytes) 0 69 (str data) set: (str bytes) 0
61 str 70 str
62 } 71 }
63 72
64 string <- { 73 string <- {
65 stringFrom: 0 to: 74 stringFrom: 0 to: length
66 } 75 }
67 76
68 findChar:else <- :char found :else { 77 findChar:from:else <- :char :start found :else {
69 notfound <- true 78 notfound <- true
70 n <- length 79 n <- length
71 i <- 0 80 i <- start
72 while: { notFound && i < n } do: { 81 while: { notfound && i < n } do: {
73 if: (get: i) = char { 82 if: (get: i) = char {
74 notfound <- false 83 notfound <- false
75 } else: { 84 } else: {
76 i <- i + 1 85 i <- i + 1
77 } 86 }