Class: RtlString

Inherits:
Object
  • Object
show all
Defined in:
lib/rtl_string.rb,
lib/rtl-string/version.rb

Constant Summary collapse

RTL_RANGES =
[
  0x0600..0x06FF,
  0x0750..0x077F,
  0x0590..0x05FF,
  0xFE70..0xFEFF,
]
VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ RtlString

Returns a new instance of RtlString.



15
16
17
# File 'lib/rtl_string.rb', line 15

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



13
14
15
# File 'lib/rtl_string.rb', line 13

def string
  @string
end

Instance Method Details

#[](index_or_range) ⇒ Object



49
50
51
52
53
# File 'lib/rtl_string.rb', line 49

def [](index_or_range)
  code_points = string.localize.code_points
  selected = code_points[index_or_range]
  self.class.new((selected.is_a?(Array) ? selected : [selected]).pack("U*"))
end

#delete_at(index) ⇒ Object



33
34
35
36
37
38
# File 'lib/rtl_string.rb', line 33

def delete_at(index)
  code_points = string.localize.code_points
  deleted = code_points.delete_at(index)
  @string = code_points.pack("U*")
  self.class.new([deleted].pack("U*"))
end

#delete_range(range) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rtl_string.rb', line 40

def delete_range(range)
  code_points = string.localize.code_points
  range.each do |i|
    code_points.delete_at(range.first)
  end
  @string = code_points.pack("U*")
  nil
end

#insert(str, index) ⇒ Object



55
56
57
58
59
60
# File 'lib/rtl_string.rb', line 55

def insert(str, index)
  code_points = string.localize.code_points
  code_points.insert(index, *str.localize.code_points)
  @string = code_points.pack("U*")
  self
end

#inspectObject



29
30
31
# File 'lib/rtl_string.rb', line 29

def inspect
  to_s.inspect
end

#sizeObject Also known as: length



62
63
64
# File 'lib/rtl_string.rb', line 62

def size
  string.localize.size
end

#to_sObject



19
20
21
22
23
24
25
26
27
# File 'lib/rtl_string.rb', line 19

def to_s
  string.localize.code_points.inject("") do |ret, code_point|
    ret << if is_rtl?(code_point)
      "[#{name_for(code_point)}]"
    else
      [code_point].pack("U*")
    end
  end
end