Module: Sutil

Defined in:
lib/modl/parser/sutil.rb

Overview

The MIT License (MIT)

Copyright © 2019 NUM Technology Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Class Method Summary collapse

Class Method Details

.after(str, c) ⇒ Object

Get everything after char c



51
52
53
54
55
56
57
58
59
# File 'lib/modl/parser/sutil.rb', line 51

def self.after(str, c)
  return if str.nil?
  return str if c.nil?

  i = str.index(c)
  return '' if i.nil?

  str.slice(i + c.length, str.length)
end

.between(str, ch1, ch2) ⇒ Object

Get everything between ch1 and ch2 ch1 and ch2 must be different and ch1 must be before ch2 in the string e.g. Sutil.between(‘func(p1,p2)’, ‘(’, ‘)’) returns ‘p1,p2’



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/modl/parser/sutil.rb', line 64

def self.between(str, ch1, ch2)
  return if str.nil?
  return str if ch1.nil? || ch2.nil? || ch1 == '' || ch2 == ''
  return str if str.length < 3

  first = str.index(ch1)
  second = str.rindex(ch2)
  if first > second
    tmp = first
    first = second
    second = tmp
  end
  return str if first == second

  str.slice(first + 1, second - first - 1)
end

.head(str, n = nil) ⇒ Object

Keep the first n characters of a string



34
35
36
37
38
39
40
# File 'lib/modl/parser/sutil.rb', line 34

def self.head(str, n = nil)
  return if str.nil?

  n = str.length - 1 if n.nil? # Strip last char by default

  str&.slice(0, n)
end

.replace(str, old, new) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/modl/parser/sutil.rb', line 89

def self.replace(str, old, new)
  return str if old.nil? || new.nil? || old.empty? || old == new

  result = str
  loop do
    break unless result.include?(old)

    result = result.sub(old, new)
  end
  result
end

.tail(str, n = 1) ⇒ Object

Remove the first n characters from a string



27
28
29
30
31
# File 'lib/modl/parser/sutil.rb', line 27

def self.tail(str, n = 1)
  return if n.negative?

  str&.slice(n, str.length)
end

.toptail(str) ⇒ Object

Remove the first and last chars from a string.



82
83
84
85
86
87
# File 'lib/modl/parser/sutil.rb', line 82

def self.toptail(str)
  return str if str.nil?
  return '' if str.length < 3

  str&.slice(1, str.length - 2)
end

.unquote(str) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/modl/parser/sutil.rb', line 101

def self.unquote(str)
  new_str = str
  if (str.start_with?('`') && str.end_with?('`')) || (str.start_with?('"') && str.end_with?('"'))
    new_str = Sutil.toptail(str)
  end
  new_str
end

.until(str, c) ⇒ Object

Keep everything until char c



43
44
45
46
47
48
# File 'lib/modl/parser/sutil.rb', line 43

def self.until(str, c)
  return str if c.nil? || c.empty?

  i = str.index(c)
  i ? str&.slice(0, i) : str
end