string_tools 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/string_tools/core_ext/string.rb +117 -0
- data/lib/string_tools/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzJkYTUyMTM2ODcwZGMzYjg3NWQ1ZTE4MTQ2YzEyYmY1MjdhMzg1MA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ODJhNTU1Mzg5YTNkNTNkMjJkNmRlZjk5MzRhN2QzOTNjNjEyYjYwYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YzFjYWZlNzY5M2ZmZGU2MDYyZWQ0ZjJlZmM3NDhiMzE5MzMyYTU5MGJhNmY0
|
10
|
+
YjIzMTI2YzBkNDk0MjdlZjA2MWRjMzU0MWM5NmE4NTE3OGY4N2RhNzYxNmFk
|
11
|
+
NjFlMzNlYWY5YWVlMzIwYzc4NzhjODY0ZTk1ZGI3MmYzMzllYjU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDQyOTRjNTQwZTU5ODViMmRhMWI5NGJhMDBmNWYwYTMxOWRiNDBjYmIwOWE1
|
14
|
+
NTI4Y2U0MGQ3MTEwMWM3MDU0NTlhYjIzZmQyNGE3MGU2OWNmNDU4YTU4ZTFk
|
15
|
+
OTY0MWYxN2VjYjlkMjRmYzI1ODA1ODRiNzhkNzRkM2EwMTI2OTM=
|
@@ -39,12 +39,119 @@ class String
|
|
39
39
|
scan(/[^\d\.]+|[\d\.]+/).map{|f| f.match(/^\d+(\.\d+)?$/) ? f.to_f : f }
|
40
40
|
end
|
41
41
|
|
42
|
+
def self.natcmp(str1, str2)
|
43
|
+
str1, str2 = str1.dup, str2.dup
|
44
|
+
compare_expression = /^(\D*)((?:\d+(?:\.\d+)?)*)(.*)$/
|
45
|
+
|
46
|
+
# Remove all whitespace
|
47
|
+
str1.gsub!(/\s*/, '')
|
48
|
+
str2.gsub!(/\s*/, '')
|
49
|
+
|
50
|
+
while (str1.length > 0) or (str2.length > 0)
|
51
|
+
# Extract non-digits, digits and rest of string
|
52
|
+
str1 =~ compare_expression
|
53
|
+
chars1, num1, str1 = $1.dup, $2.dup, $3.dup
|
54
|
+
|
55
|
+
str2 =~ compare_expression
|
56
|
+
chars2, num2, str2 = $1.dup, $2.dup, $3.dup
|
57
|
+
|
58
|
+
# Compare the non-digits
|
59
|
+
case (chars1 <=> chars2)
|
60
|
+
when 0 # Non-digits are the same, compare the digits...
|
61
|
+
# If either number begins with a zero, then compare alphabetically,
|
62
|
+
# otherwise compare numerically
|
63
|
+
if !(num1[0] == 48 && num1[1] != 46) and !(num2[0] == 48 && num2[1] != 46)
|
64
|
+
num1, num2 = num1.to_f, num2.to_f
|
65
|
+
end
|
66
|
+
|
67
|
+
case (num1 <=> num2)
|
68
|
+
when -1 then return -1
|
69
|
+
when 1 then return 1
|
70
|
+
end
|
71
|
+
when -1 then return -1
|
72
|
+
when 1 then return 1
|
73
|
+
end # case
|
74
|
+
|
75
|
+
end # while
|
76
|
+
|
77
|
+
# Strings are naturally equal
|
78
|
+
0
|
79
|
+
end
|
42
80
|
|
43
81
|
# Выполняет преобразование строки в punycode.
|
44
82
|
def to_punycode
|
45
83
|
Addressable::URI.parse(self).normalize.to_s
|
46
84
|
end
|
47
85
|
|
86
|
+
# Embed in a String to clear all previous ANSI sequences.
|
87
|
+
ANSI_CLEAR = "\e[0m"
|
88
|
+
ANSI_BOLD = "\e[1m"
|
89
|
+
ANSI_UNDERLINE = "\e[4m"
|
90
|
+
|
91
|
+
# Colors
|
92
|
+
BLACK = "\e[30m"
|
93
|
+
RED = "\e[31m"
|
94
|
+
GREEN = "\e[32m"
|
95
|
+
YELLOW = "\e[33m"
|
96
|
+
BLUE = "\e[34m"
|
97
|
+
MAGENTA = "\e[35m"
|
98
|
+
CYAN = "\e[36m"
|
99
|
+
WHITE = "\e[37m"
|
100
|
+
|
101
|
+
# === Synopsys
|
102
|
+
# Colorize string (for terminals)
|
103
|
+
# Does not work with sprintf yet
|
104
|
+
#
|
105
|
+
# === Usage
|
106
|
+
# "ln -s".colorize(:red)
|
107
|
+
#
|
108
|
+
# === Args
|
109
|
+
# +color+ - symbol, one of the following (black, white, red, green, yellow, blue, magenta, cyan)
|
110
|
+
# +bold_or_options+ - True/False or Hash
|
111
|
+
def colorize(color, bold_or_options = nil)
|
112
|
+
is_bold = bold_or_options.is_a?(TrueClass)
|
113
|
+
is_underline = false
|
114
|
+
|
115
|
+
if bold_or_options.is_a?(Hash)
|
116
|
+
is_bold ||= bold_or_options[:bold]
|
117
|
+
is_underline = bold_or_options[:underline]
|
118
|
+
end
|
119
|
+
|
120
|
+
raise ArgumentError('Color must be a symbol') unless color.is_a?(Symbol)
|
121
|
+
color_const = color.to_s.upcase.to_sym
|
122
|
+
|
123
|
+
raise ArgumentError('Unknown color') unless self.class.const_defined?(color_const)
|
124
|
+
ascii_color = self.class.const_get(color_const)
|
125
|
+
|
126
|
+
s = surround_with_ansi(ascii_color)
|
127
|
+
s = s.bold if is_bold
|
128
|
+
s = s.underline if is_underline
|
129
|
+
s
|
130
|
+
end
|
131
|
+
|
132
|
+
# === Synopsys
|
133
|
+
# Make text bolder (for ASCII terminals)
|
134
|
+
def bold
|
135
|
+
surround_with_ansi(ANSI_BOLD)
|
136
|
+
end
|
137
|
+
|
138
|
+
# === Synopsys
|
139
|
+
# Make text underlined (for ASCII terminals)
|
140
|
+
def underline
|
141
|
+
surround_with_ansi(ANSI_UNDERLINE)
|
142
|
+
end
|
143
|
+
|
144
|
+
# === Synopsys
|
145
|
+
# remove colors from colorized string
|
146
|
+
def remove_colors
|
147
|
+
gsub(/\e\[\d+m/, '')
|
148
|
+
end
|
149
|
+
|
150
|
+
[:black, :white, :red, :green, :yellow, :blue, :magenta, :cyan].each do |color|
|
151
|
+
define_method color do |*args|
|
152
|
+
colorize(color, *args)
|
153
|
+
end
|
154
|
+
end
|
48
155
|
|
49
156
|
# shorthand
|
50
157
|
def detect_encoding
|
@@ -84,4 +191,14 @@ class String
|
|
84
191
|
def to_cp1251!
|
85
192
|
self.replace(self.to_cp1251)
|
86
193
|
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def surround_with_ansi(ascii_seq)
|
198
|
+
"#{ascii_seq}#{protect_escape_of(ascii_seq)}#{ANSI_CLEAR}"
|
199
|
+
end
|
200
|
+
|
201
|
+
def protect_escape_of(ascii_seq)
|
202
|
+
gsub(Regexp.new(Regexp.escape(ANSI_CLEAR)), "#{ANSI_CLEAR}#{ascii_seq}")
|
203
|
+
end
|
87
204
|
end
|
data/lib/string_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey D.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|