annotate modules/string.tp @ 341:6871e72b6db2

Added int64 message to string type
author Michael Pavone <pavone@retrodev.com>
date Sun, 05 Apr 2015 22:48:59 -0700
parents 74cab9b5f2a4
children
rev   line source
pavone@88 1 #{
pavone@88 2 llProperty: len withType: uint32_t
pavone@88 3 llProperty: bytes withType: uint32_t
pavone@88 4 llProperty: data withType: (char ptr)
pavone@147 5
pavone@88 6 llMessage: length withVars: {
pavone@88 7 intret <- (obj_int32 ptr)
pavone@88 8 } andCode: {
pavone@88 9 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@88 10 intret num!: len
pavone@88 11 intret
pavone@88 12 }
pavone@147 13
pavone@88 14 llMessage: byte_length withVars: {
pavone@88 15 intret <- (obj_int32 ptr)
pavone@88 16 } andCode: {
pavone@88 17 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@88 18 intret num!: bytes
pavone@88 19 intret
pavone@88 20 }
pavone@147 21
pavone@265 22 llMessage: "=" withVars: {
pavone@88 23 argb <- (string ptr)
pavone@88 24 } andCode: :argb {
pavone@88 25 if: len = (argb len) && bytes = (argb bytes) && (not: (memcmp: data (argb data) bytes)) {
pavone@88 26 true
pavone@88 27 }
pavone@88 28 }
pavone@147 29
pavone@243 30 llMessage: compareSub withVars: {
pavone@243 31 argb <- string ptr
pavone@243 32 myoff <- obj_int32 ptr
pavone@243 33 boff <- obj_int32 ptr
pavone@243 34 clen <- obj_int32 ptr
pavone@243 35 intret <- obj_int32 ptr
pavone@243 36 } andCode: :argb myoff boff clen {
pavone@243 37 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@243 38 intret num!: (memcmp: data + (myoff num) (argb data) + (boff num) (clen num))
pavone@243 39 intret
pavone@243 40 }
pavone@243 41
pavone@265 42 llMessage: "!=" withVars: {
pavone@88 43 argb <- (string ptr)
pavone@88 44 } andCode: :argb {
pavone@88 45 if: len != (argb len) || bytes != (argb bytes) || (memcmp: data (argb data) bytes) {
pavone@88 46 true
pavone@88 47 }
pavone@88 48 }
pavone@147 49
pavone@295 50 print <- {
pavone@295 51 (file stdout) write: self
pavone@88 52 self
pavone@88 53 }
pavone@147 54
pavone@88 55 llMessage: string withVars: {} andCode: {
pavone@88 56 self
pavone@88 57 }
pavone@147 58
pavone@265 59 llMessage: "." withVars: {
pavone@88 60 argbo <- (object ptr)
pavone@88 61 argb <- (string ptr)
pavone@88 62 out <- (string ptr)
pavone@88 63 } andCode: :argbo {
pavone@267 64 argb <- (mcall: string 1 argbo) castTo: (string ptr)
pavone@88 65 out <- make_object: (addr_of: string_meta) NULL 0
pavone@88 66 out bytes!: bytes + (argb bytes)
pavone@88 67 out len!: len + (argb len)
pavone@88 68 out data!: (GC_MALLOC_ATOMIC: (out bytes) + 1)
pavone@88 69 memcpy: (out data) data bytes
pavone@88 70 memcpy: (out data) + bytes (argb data) (argb bytes) + 1
pavone@88 71 out
pavone@88 72 }
pavone@147 73
pavone@88 74 llMessage: byte withVars: {
pavone@88 75 index <- (obj_int32 ptr)
pavone@88 76 intret <- (obj_int32 ptr)
pavone@88 77 } andCode: :index {
pavone@88 78 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@88 79 intret num!: (if: (index num) < bytes { data get: (index num) } else: {0})
pavone@88 80 intret
pavone@88 81 }
pavone@147 82
pavone@88 83 llMessage: int32 withVars: {
pavone@88 84 intret <- (obj_int32 ptr)
pavone@88 85 } andCode: {
pavone@88 86 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@88 87 intret num!: (atoi: data)
pavone@88 88 intret
pavone@88 89 }
pavone@341 90
pavone@341 91 llMessage: int64 withVars: {
pavone@341 92 int64ret <- (obj_int64 ptr)
pavone@341 93 } andCode: {
pavone@341 94 int64ret <- make_object: (addr_of: obj_int64_meta) NULL 0
pavone@341 95 int64ret num!: (atoll: data)
pavone@341 96 int64ret
pavone@341 97 }
pavone@147 98
pavone@158 99 parseHex32 <- {
pavone@158 100 num <- 0u32
pavone@158 101 cur <- 0
pavone@158 102 a <- uint32: ("a" byte: 0)
pavone@158 103 A <- uint32: ("A" byte: 0)
pavone@158 104 f <- uint32: ("f" byte: 0)
pavone@158 105 F <- uint32: ("F" byte: 0)
pavone@158 106 zero <- "0" byte: 0
pavone@158 107 nine <- "9" byte: 0
pavone@158 108 while: { cur < byte_length} do: {
pavone@158 109 b <- uint32: (byte: cur)
pavone@158 110 cur <- cur + 1
pavone@158 111 if: b >= zero && b <= nine {
pavone@158 112 num <- num * 16 + (b - zero)
pavone@158 113 } else: {
pavone@158 114 if: b >= a && b <= f {
pavone@158 115 num <- num * 16 + (b - a) + 10u32
pavone@158 116 } else: {
pavone@158 117 if: b >= A && b <= F {
pavone@158 118 num <- num * 16 + (b - A) + 10u32
pavone@158 119 } else: {
pavone@158 120 cur <- byte_length
pavone@158 121 }
pavone@158 122 }
pavone@158 123 }
pavone@158 124 }
pavone@158 125 num
pavone@158 126 }
pavone@158 127
pavone@158 128 parseHex64 <- {
pavone@158 129 num <- 0u64
pavone@158 130 cur <- 0
pavone@158 131 a <- uint64: ("a" byte: 0)
pavone@158 132 A <- uint64: ("A" byte: 0)
pavone@158 133 f <- uint64: ("f" byte: 0)
pavone@158 134 F <- uint64: ("F" byte: 0)
pavone@158 135 zero <- "0" byte: 0
pavone@158 136 nine <- "9" byte: 0
pavone@158 137 while: { cur < byte_length} do: {
pavone@158 138 b <- uint64: (byte: cur)
pavone@158 139 cur <- cur + 1
pavone@158 140 if: b >= zero && b <= nine {
pavone@158 141 num <- num * 16 + (b - zero)
pavone@158 142 } else: {
pavone@158 143 if: b >= a && b <= f {
pavone@158 144 num <- num * 16 + (b - a) + 10u64
pavone@158 145 } else: {
pavone@158 146 if: b >= A && b <= F {
pavone@158 147 num <- num * 16 + (b - A) + 10u64
pavone@158 148 } else: {
pavone@158 149 cur <- byte_length
pavone@158 150 }
pavone@158 151 }
pavone@158 152 }
pavone@158 153 }
pavone@158 154 num
pavone@158 155 }
pavone@339 156
pavone@339 157 llMessage: parseFloat64 withVars: {
pavone@339 158 fret64 <- obj_float64 ptr
pavone@339 159 } andCode: {
pavone@339 160 //TODO: This should probably detect non-numeric values and return an option type
pavone@339 161 fret64 <- make_object: (addr_of: obj_float64_meta) NULL 0
pavone@339 162 fret64 num!: (atof: data)
pavone@339 163 fret64
pavone@339 164 }
pavone@339 165
pavone@339 166 parseFloat32 <- {
pavone@339 167 parseFloat64 f32
pavone@339 168 }
pavone@158 169
pavone@88 170 llMessage: hash withVars: {
pavone@88 171 intret <- (obj_int32 ptr)
pavone@88 172 i <- uint32_t
pavone@88 173 } andCode: {
pavone@88 174 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@88 175 intret num!: 0
pavone@88 176 if: bytes {
pavone@88 177 intret num!: (data get: 0) * 128
pavone@88 178 i <- 0
pavone@88 179 while: { i < bytes } do: {
pavone@88 180 intret num!: (1000003 * (intret num)) xor (data get: i)
pavone@88 181 i <- i + 1
pavone@88 182 }
pavone@88 183 intret num!: (intret num) xor bytes
pavone@88 184 }
pavone@88 185 intret
pavone@88 186 }
pavone@147 187
pavone@154 188 llMessage: find:startingAt:else withVars: {
pavone@150 189 intret <- obj_int32 ptr
pavone@150 190 oneedle <- object ptr
pavone@154 191 startpos <- obj_int32 ptr
pavone@150 192 ifNotFound <- object ptr
pavone@150 193 sneedle <- string ptr
pavone@150 194 i <- uint32_t
pavone@150 195 notFound <- uint32_t
pavone@154 196 } andCode: :oneedle :startpos :ifNotFound {
pavone@267 197 sneedle <- (mcall: string 1 oneedle) castTo: (string ptr)
pavone@154 198 i <- startpos num
pavone@150 199 notFound <- 1
pavone@150 200 while: { notFound && i + (sneedle bytes) <= bytes} do: {
pavone@150 201 if: (memcmp: data + i (sneedle data) (sneedle bytes)) = 0 {
pavone@150 202 notFound <- 0
pavone@150 203 } else: {
pavone@150 204 i <- i + 1
pavone@150 205 }
pavone@150 206 }
pavone@150 207 if: notFound {
pavone@150 208 ccall: ifNotFound 0
pavone@150 209 } else: {
pavone@150 210 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@150 211 intret num!: i
pavone@150 212 intret
pavone@150 213 }
pavone@150 214 }
pavone@150 215
pavone@154 216 find:else <- :toFind :orElse {
pavone@154 217 find: toFind startingAt: 0 else: orElse
pavone@154 218 }
pavone@154 219
pavone@151 220 llMessage: from:withLength withVars: {
pavone@151 221 from <- obj_int32 ptr
pavone@151 222 tocopy <- obj_int32 ptr
pavone@151 223 ret <- string ptr
pavone@151 224 start <- int32_t
pavone@151 225 clampedLen <- int32_t
pavone@151 226 } andCode: :from :tocopy {
pavone@151 227 start <- from num
pavone@151 228 if: start < 0 {
pavone@151 229 start <- bytes + start
pavone@151 230 }
pavone@151 231 if: start > bytes {
pavone@151 232 start <- bytes
pavone@151 233 }
pavone@151 234 clampedLen <- tocopy num
pavone@306 235 if: clampedLen < 0 {
pavone@306 236 clampedLen <- bytes - clampedLen
pavone@306 237 if: clampedLen < 0 {
pavone@306 238 clampedLen <- 0
pavone@306 239 }
pavone@306 240 }
pavone@151 241 if: start + clampedLen > bytes {
pavone@151 242 clampedLen <- bytes - start
pavone@151 243 }
pavone@151 244 ret <- make_object: (addr_of: string_meta) NULL 0
pavone@151 245 ret data!: (GC_MALLOC_ATOMIC: clampedLen + 1)
pavone@151 246 memcpy: (ret data) data + start clampedLen
pavone@151 247 ret len!: clampedLen
pavone@151 248 ret bytes!: clampedLen
pavone@305 249 (ret data) set: clampedLen 0
pavone@151 250 ret
pavone@151 251 }
pavone@151 252
pavone@151 253 from <- :start {
pavone@151 254 from: start withLength: length
pavone@151 255 }
pavone@151 256
pavone@152 257 partitionOn <- :delim {
pavone@152 258 pos <- find: delim else: { -1 }
pavone@152 259 if: pos >= 0 {
pavone@152 260 _before <- from: 0 withLength: pos
pavone@152 261 _after <- from: (pos + (delim length))
pavone@152 262 #{
pavone@152 263 before <- _before
pavone@152 264 after <- _after
pavone@152 265 }
pavone@152 266 } else: {
pavone@152 267 _before <- self
pavone@152 268 #{
pavone@152 269 before <- _before
pavone@152 270 after <- ""
pavone@152 271 }
pavone@152 272 }
pavone@152 273 }
pavone@152 274
pavone@152 275 splitOn <- :delim {
pavone@152 276 pos <- 0
pavone@152 277 pieces <- #[]
pavone@152 278 while: {
pavone@152 279 pos <- find: delim else: { -1 }
pavone@152 280 pos >= 0
pavone@152 281 } do: {
pavone@152 282 pieces append: (from: 0 withLength: pos)
pavone@152 283 self <- from: pos + (delim length)
pavone@152 284 }
pavone@152 285 pieces append: self
pavone@152 286 }
pavone@152 287
pavone@259 288 ltrim <- {
pavone@259 289 l <- length
pavone@259 290 start <- 0
pavone@259 291 space <- " " byte: 0
pavone@259 292 tab <- "\t" byte: 0
pavone@259 293 nl <- "\n" byte: 0
pavone@259 294 cr <- "\r" byte: 0
pavone@259 295
pavone@259 296 while: {
pavone@259 297 if: start < l {
pavone@259 298 b <- byte: start
pavone@259 299 b = space || b = tab || b = nl || b = cr
pavone@259 300 }
pavone@259 301 } do: {
pavone@259 302 start <- start + 1
pavone@259 303 }
pavone@259 304 from: start
pavone@259 305 }
pavone@259 306
pavone@157 307 trim <- {
pavone@157 308 l <- length
pavone@157 309 start <- 0
pavone@157 310 space <- " " byte: 0
pavone@157 311 tab <- "\t" byte: 0
pavone@157 312 nl <- "\n" byte: 0
pavone@157 313 cr <- "\r" byte: 0
pavone@157 314
pavone@157 315 while: {
pavone@157 316 if: start < l {
pavone@157 317 b <- byte: start
pavone@157 318 b = space || b = tab || b = nl || b = cr
pavone@157 319 }
pavone@157 320 } do: {
pavone@157 321 start <- start + 1
pavone@157 322 }
pavone@306 323 end <- l - 1
pavone@157 324 while: {
pavone@306 325 if: end > start {
pavone@306 326 b <- byte: end
pavone@157 327 b = space || b = tab || b = nl || b = cr
pavone@157 328 }
pavone@157 329 } do: {
pavone@295 330 end <- end - 1
pavone@157 331 }
pavone@306 332 from: start withLength: (end + 1 - start)
pavone@157 333 }
pavone@301 334
pavone@301 335 startsWith? <- :prefix {
pavone@301 336 if: (prefix length) <= length {
pavone@301 337 0 = (compareSub: prefix 0 0 (prefix length))
pavone@301 338 }
pavone@301 339 }
pavone@157 340
pavone@270 341 endsWith? <- :suffix {
pavone@270 342 if: (suffix length) <= length {
pavone@270 343 0 = (compareSub: suffix (length - (suffix length)) 0 (suffix length))
pavone@270 344 }
pavone@270 345 }
pavone@270 346
pavone@271 347 jsonEncode <- {
pavone@271 348 i <- 0
pavone@271 349 start <- 0
pavone@271 350 parts <- #["\""]
pavone@271 351 q <- "\"" byte: 0
pavone@271 352 s <- "\\" byte: 0
pavone@271 353 while: { i < byte_length } do: {
pavone@271 354 b <- byte: i
pavone@271 355 if: b = q {
pavone@271 356 parts append: (from: start withLength: i - start)
pavone@271 357 start <- i + 1
pavone@271 358 parts append: "\\\""
pavone@271 359 } else: {
pavone@271 360 if: b = s {
pavone@271 361 parts append: (from: start withLength: i - start)
pavone@271 362 start <- i + 1
pavone@271 363 parts append: "\\\\"
pavone@271 364 }
pavone@271 365 }
pavone@271 366
pavone@271 367 i <- i + 1
pavone@271 368 }
pavone@271 369 if: start < byte_length {
pavone@271 370 parts append: (from: start)
pavone@271 371 }
pavone@271 372 parts append: "\""
pavone@271 373 parts join: ""
pavone@271 374 }
pavone@271 375
pavone@147 376 isInteger? <- { false }
pavone@154 377 isString? <- { true }
pavone@243 378 isBasicString? <- { true }
pavone@88 379 }