# HG changeset patch # User Mike Pavone # Date 1343055500 25200 # Node ID 474f17ebaaa07a84c28b266a8e581e36359d147e # Parent 25bc8a5ab41ef563e6928f33feb1ea5a9f5e37eb Add string.tp which should have been in previous commit diff -r 25bc8a5ab41e -r 474f17ebaaa0 modules/string.tp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/string.tp Mon Jul 23 07:58:20 2012 -0700 @@ -0,0 +1,96 @@ +#{ + llProperty: len withType: uint32_t + llProperty: bytes withType: uint32_t + llProperty: data withType: (char ptr) + + llMessage: length withVars: { + intret <- (obj_int32 ptr) + } andCode: { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: len + intret + } + + llMessage: byte_length withVars: { + intret <- (obj_int32 ptr) + } andCode: { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: bytes + intret + } + + llMessage: EQ_ withVars: { + argb <- (string ptr) + } andCode: :argb { + if: len = (argb len) && bytes = (argb bytes) && (not: (memcmp: data (argb data) bytes)) { + true + } + } + + llMessage: NEQ_ withVars: { + argb <- (string ptr) + } andCode: :argb { + if: len != (argb len) || bytes != (argb bytes) || (memcmp: data (argb data) bytes) { + true + } + } + + llMessage: print withVars: {} andCode: { + fwrite: data 1 bytes stdout + self + } + + llMessage: string withVars: {} andCode: { + self + } + + llMessage: CAT_ withVars: { + argbo <- (object ptr) + argb <- (string ptr) + out <- (string ptr) + } andCode: :argbo { + argb <- mcall: string 1 argbo + out <- make_object: (addr_of: string_meta) NULL 0 + out bytes!: bytes + (argb bytes) + out len!: len + (argb len) + out data!: (GC_MALLOC_ATOMIC: (out bytes) + 1) + memcpy: (out data) data bytes + memcpy: (out data) + bytes (argb data) (argb bytes) + 1 + out + } + + llMessage: byte withVars: { + index <- (obj_int32 ptr) + intret <- (obj_int32 ptr) + } andCode: :index { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: (if: (index num) < bytes { data get: (index num) } else: {0}) + intret + } + + llMessage: int32 withVars: { + intret <- (obj_int32 ptr) + } andCode: { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: (atoi: data) + intret + } + + llMessage: hash withVars: { + intret <- (obj_int32 ptr) + i <- uint32_t + } andCode: { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: 0 + if: bytes { + intret num!: (data get: 0) * 128 + i <- 0 + while: { i < bytes } do: { + intret num!: (1000003 * (intret num)) xor (data get: i) + i <- i + 1 + } + intret num!: (intret num) xor bytes + } + intret + } +}