Module: Kl::Primitives::Strings

Included in:
Environment
Defined in:
lib/kl/primitives/strings.rb

Overview

For the time being, Shen Ruby’s string functions only support 8-bit characters. Once the Shen environment is up and running and passing its test suite, strings will be extended to support UTF-8.

Instance Method Summary collapse

Instance Method Details

#cn(s1, s2) ⇒ Object

Raises:



22
23
24
25
26
# File 'lib/kl/primitives/strings.rb', line 22

def cn(s1, s2)
  raise Kl::Error, "#{s1} is not a string" unless s1.kind_of? String
  raise Kl::Error, "#{s2} is not a string" unless s2.kind_of? String
  s1 + s2
end

#pos(s, n) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
# File 'lib/kl/primitives/strings.rb', line 7

def pos(s, n)
  raise Kl::Error, "#{s} is not a string" unless s.kind_of? String
  raise Kl::Error, "#{n} is not an integer" unless n.kind_of? Fixnum
  if n < 0 || n >= s.length
    raise Kl::Error, "out of bounds"
  end
  s.byteslice(n)
end

#str(x) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kl/primitives/strings.rb', line 28

def str(x)
  case x
  when String
    '"' + x + '"'
  when Symbol
    x.to_s
  when Numeric
    x.to_s
  when TrueClass, FalseClass
    x.to_s
  when Proc
    x.to_s
  when IO
    x.to_s
  else
    raise Kl::Error, "str applied to non-atomic type: #{x.class}"
  end
end

#string?(x) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/kl/primitives/strings.rb', line 47

def string?(x)
  x.kind_of? String
end

#tlstr(s) ⇒ Object

Raises:



16
17
18
19
20
# File 'lib/kl/primitives/strings.rb', line 16

def tlstr(s)
  raise Kl::Error, "#{s} is not a string" unless s.kind_of? String
  raise Kl::Error, "attempted to take tail of an empty string" if s.empty?
  s.byteslice(1, s.bytesize - 1)
end