Class: String

Inherits:
Object show all
Defined in:
lib/rlang/lib/kernel.rb,
lib/rlang/lib/string.rb,
lib/rlang/parser/ext/string.rb

Overview

Rubinius WebAssembly VM Copyright © 2019-2020, Laurent Julliard and contributors All rights reserved.

Kernel methods

Most of the Kernel methods are defined in other files to avoid requiring classes that rely on Kernel methods themselves.

Direct Known Subclasses

RString

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, length) ⇒ String

str is a pointer to a memory location of type :String allocated either statically through a string litteral or through a dynamic allocation ptr is a simple memory address of type :I32 (see it as the equivalent of a char * in C)

There are 3 ways to initialize a new String object

* with a string literal (e.g. mystring = "Hello World!")
* by pointing at an existing memory location (e.g. String.new(ptr, length))
* by asking Rlang to allocate the String space when ptr is NULL (e.g. String.new(0, length))

No memory is allocated for the string bytes if the string is empty



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rlang/lib/string.rb', line 20

def initialize(ptr, length)
  result :none
  if ptr == 0
    if length == 0
      @ptr = 0
    else
      @ptr = Malloc.malloc(length)
    end
  else
    @ptr = ptr
  end
  @length = length
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



5
6
7
# File 'lib/rlang/lib/string.rb', line 5

def length
  @length
end

#ptrObject (readonly)

Returns the value of attribute ptr.



5
6
7
# File 'lib/rlang/lib/string.rb', line 5

def ptr
  @ptr
end

Instance Method Details

#!=(stg) ⇒ Object



123
124
125
126
# File 'lib/rlang/lib/string.rb', line 123

def !=(stg)
  arg stg: :String
  !(self == stg)
end

#*(times) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rlang/lib/string.rb', line 86

def *(times)
  result :String
  full_length = @length * times
  stg = String.new(0, full_length)
  idx=0
  while idx < full_length
    stg[idx] = self
    idx += @length
  end
  stg
end

#+(stg) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rlang/lib/string.rb', line 46

def +(stg)
  arg stg: :String
  result :String

  # Create new object string with proper size
  s = String.new(0, @length + stg.length)
  # Copy both strings in the new one
  Memory.copy(@ptr, s.ptr, @length)
  Memory.copy(stg.ptr, s.ptr + @length, stg.length)
  s
end

#==(stg) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rlang/lib/string.rb', line 111

def ==(stg)
  arg stg: :String
  return false if stg.length != @length
  i = 0
  stg_ptr = stg.ptr
  while i < @length
    return false if Memory.load32_8(@ptr+i) != Memory.load32_8(stg_ptr+i)
    i += 1
  end
  true
end

#[](idx) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rlang/lib/string.rb', line 58

def [](idx)
  result :String
  # The condition below should actually return nil
  # to be compliant with the Ruby library but we don't
  # have nil in Rlang so return an empty string
  return "" if (idx >= @length) || (idx < -@length)
  idx = (@length + idx) if idx < 0
  stg = String.new(0,1)
  Memory.copy(@ptr+idx, stg.ptr, 1)
  stg
end

#[]=(idx, stg) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rlang/lib/string.rb', line 70

def []=(idx, stg)
  arg stg: :String
  result :String
  if (idx >= @length) || (idx < -@length)
    raise "IndexError: index out bound"
  end
  idx = (@length + idx) if idx < 0
  i=0
  tgt_ptr = @ptr+idx
  while i < stg.length && (idx + i) < @length
    Memory.copy(stg.ptr+i, tgt_ptr+i, 1)
    i += 1
  end
  stg
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rlang/lib/string.rb', line 37

def empty?
  @length == 0
end

#ordObject



41
42
43
44
# File 'lib/rlang/lib/string.rb', line 41

def ord
  result :I32
  Memory.load32_8(@ptr)
end

#reverse!Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rlang/lib/string.rb', line 98

def reverse!
  result :String
  half_size = @length/2
  i=0
  while i < half_size
    swap = Memory.load32_8(@ptr+i)
    Memory.store32_8(@ptr+i, Memory.load32_8(@ptr+@length-1-i))
    Memory.store32_8(@ptr+@length-1-i, swap)
    i += 1
  end
  self
end

#sizeObject



34
# File 'lib/rlang/lib/string.rb', line 34

def size; @length; end

#to_sObject



35
# File 'lib/rlang/lib/string.rb', line 35

def to_s; self; end

#to_wasmObject



2
3
4
# File 'lib/rlang/parser/ext/string.rb', line 2

def to_wasm
  self.split('').map {|c| (32..126).include?(c.ord) ? c : "\\#{'%02X' % c.ord}"}.join('')
end