cc 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/LICENSE-CN.md +9 -0
- data/README.md +3 -0
- data/attribute.rb +71 -0
- data/cc.rb +43 -0
- data/chinese.rb +71 -0
- data/chrono.rb +162 -0
- data/enum.rb +47 -0
- data/exception.rb +91 -0
- data/file.rb +253 -0
- data/kernel.rb +227 -0
- data/monkey-patch.rb +116 -0
- data/number.rb +105 -0
- data/regexp.rb +23 -0
- data/string.rb +75 -0
- data/tree.rb +184 -0
- metadata +57 -0
data/regexp.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#coding:utf-8
|
2
|
+
|
3
|
+
############################################################################################################
|
4
|
+
# HOW TO USE #
|
5
|
+
############################################################################################################
|
6
|
+
|
7
|
+
=begin
|
8
|
+
require 'cc'
|
9
|
+
CC.use 'regexp' # immediately when loading this file
|
10
|
+
|
11
|
+
exp1 = Regexp.string 'a+b'
|
12
|
+
p 'a+b+c'.match(exp1).to_s
|
13
|
+
|
14
|
+
exp2 = Regexp.string 'a+\d'
|
15
|
+
p 'a+3+c'.match(exp2).to_s
|
16
|
+
=end
|
17
|
+
|
18
|
+
class Regexp
|
19
|
+
# 从字符串转正则式(含特殊字符,待补完...) ※ No /\\/ changed
|
20
|
+
def self.string string
|
21
|
+
string.gsub(/([\/\$\^\*\+\?\.\|\(\)\{\}\[\]\-])/){|match|"\\#{match}"}
|
22
|
+
end
|
23
|
+
end
|
data/string.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
#coding:utf-8
|
2
|
+
|
3
|
+
############################################################################################################
|
4
|
+
# HOW TO USE #
|
5
|
+
############################################################################################################
|
6
|
+
|
7
|
+
=begin
|
8
|
+
require 'cc'
|
9
|
+
CC.use 'string' # immediately when loading this file
|
10
|
+
|
11
|
+
puts "String custom strip:"
|
12
|
+
p " hello\n\t".compact, " -- hello -- ".compact_head(' ','-',' '), " -- hello- -".compact_tail('-',' ','-')
|
13
|
+
|
14
|
+
str = "\nHello, \nworld!\n"
|
15
|
+
puts "\nThe string #{str.inspect} width is #{str.width} and height is #{str.height}"
|
16
|
+
|
17
|
+
puts "\nString custom desensitize:"
|
18
|
+
puts "1234567890".desensitize("3456")
|
19
|
+
|
20
|
+
puts "\nString custom encoding:"
|
21
|
+
str = "中文"
|
22
|
+
puts "UTF-8: #{str.inspect}"
|
23
|
+
puts "GBK: #{str.force_gbk.inspect}"
|
24
|
+
puts "UTF-8: #{str.force_gbk.force_utf8.inspect}"
|
25
|
+
=end
|
26
|
+
|
27
|
+
class String
|
28
|
+
def compact *params
|
29
|
+
clone = self.clone
|
30
|
+
(params.empty? ? [" ","\t","\n"] : params).each do|param|
|
31
|
+
clone.gsub!(/^#{Regexp.escape(param)}+|#{Regexp.escape(param)}+$/, "")
|
32
|
+
end
|
33
|
+
return clone
|
34
|
+
end
|
35
|
+
|
36
|
+
def compact_tail *params
|
37
|
+
clone = self.clone
|
38
|
+
(params.empty? ? [" ","\t","\n"] : params).each do|param|
|
39
|
+
clone.gsub!(/#{Regexp.escape(param)}+$/, "")
|
40
|
+
end
|
41
|
+
return clone
|
42
|
+
end
|
43
|
+
|
44
|
+
def compact_head *params
|
45
|
+
clone = self.clone
|
46
|
+
(params.empty? ? [" ","\t","\n"] : params).each do|param|
|
47
|
+
clone.gsub!(/^#{Regexp.escape(param)}+/, "")
|
48
|
+
end
|
49
|
+
return clone
|
50
|
+
end
|
51
|
+
|
52
|
+
def effective?
|
53
|
+
!self.strip.empty?
|
54
|
+
end
|
55
|
+
|
56
|
+
def width
|
57
|
+
self.split("\n").map(&:size).max || 0
|
58
|
+
end
|
59
|
+
|
60
|
+
def height
|
61
|
+
self.split("\n").reject(&:empty?).size
|
62
|
+
end
|
63
|
+
|
64
|
+
def desensitize target,holder='*',full=:full
|
65
|
+
self.gsub(target,(full==:full ? Array.new(target.size,holder).join : holder))
|
66
|
+
end
|
67
|
+
|
68
|
+
def force_gbk
|
69
|
+
String.new(self).encode('GBK').force_encoding('ASCII-8BIT')
|
70
|
+
end
|
71
|
+
|
72
|
+
def force_utf8 default="GBK"
|
73
|
+
String.new(self).force_encoding(default).encode('UTF-8')
|
74
|
+
end
|
75
|
+
end
|
data/tree.rb
ADDED
@@ -0,0 +1,184 @@
|
|
1
|
+
#coding:utf-8
|
2
|
+
|
3
|
+
############################################################################################################
|
4
|
+
# The Class Tree is a recursive hash-generator based on Hash. #
|
5
|
+
# #
|
6
|
+
# HOW TO USE #
|
7
|
+
############################################################################################################
|
8
|
+
|
9
|
+
=begin
|
10
|
+
require 'cc'
|
11
|
+
CC.use 'tree'
|
12
|
+
|
13
|
+
# 奇怪的数据结构增加了!!!
|
14
|
+
a = Tree.init
|
15
|
+
|
16
|
+
# 逐个赋值
|
17
|
+
a[1][2][3] = :'4'
|
18
|
+
a['1']['2']['3']=4
|
19
|
+
p a
|
20
|
+
puts Array.new(32,'-').join
|
21
|
+
|
22
|
+
# 直接挂载
|
23
|
+
a.mount '1/2/3/4/5/6', '7'
|
24
|
+
p a
|
25
|
+
a['1/2/3'] = 4
|
26
|
+
p a
|
27
|
+
puts Array.new(32,'-').join
|
28
|
+
|
29
|
+
# 不能给根目录赋值
|
30
|
+
begin
|
31
|
+
a['/'] = :root
|
32
|
+
rescue Tree::PathError => exception
|
33
|
+
puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
|
34
|
+
end
|
35
|
+
begin
|
36
|
+
a.mount '/', :home
|
37
|
+
rescue Tree::PathError => exception
|
38
|
+
puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
|
39
|
+
end
|
40
|
+
p a.route('/')==a['/']
|
41
|
+
puts Array.new(32,'-').join
|
42
|
+
|
43
|
+
# 合并到某分支,连接到路径
|
44
|
+
b = Tree.init
|
45
|
+
b['a/b/c'] = :d
|
46
|
+
a.emerge '1/2/3', b['a']
|
47
|
+
p a # a['1/2/b'].parent==a['1/2/3'].parent # a['1/2/3'] is 4 and not respond to parent
|
48
|
+
begin
|
49
|
+
a.contact '1/2/3', b['a']
|
50
|
+
rescue Tree::PathError => exception
|
51
|
+
puts "#{exception.class}: #{exception.message}\n #{exception.backtrace.join("\n ")}"
|
52
|
+
end
|
53
|
+
a.contact '1/2', b
|
54
|
+
p a
|
55
|
+
puts Array.new(32,'-').join
|
56
|
+
|
57
|
+
# 回溯父节点
|
58
|
+
t = Tree.init
|
59
|
+
p t['a/b/c'] = :d
|
60
|
+
p t
|
61
|
+
puts Array.new(32,'-').join
|
62
|
+
p '1:',t['1'],t['1/'].parent
|
63
|
+
puts Array.new(32,'-').join
|
64
|
+
p '1/2:',t['/1/2'],t['1/2'].parent
|
65
|
+
puts Array.new(32,'-').join
|
66
|
+
p '1/2/3:',t['1/2/3/'],t['1/2/3'].parent
|
67
|
+
puts Array.new(32,'-').join
|
68
|
+
p '1/2/3/4:',t['1/2/3/4'],t['1/2/3/4'].parent
|
69
|
+
puts Array.new(32,'-').join
|
70
|
+
p 'a:',t['a'],t['a'].parent
|
71
|
+
puts Array.new(32,'-').join
|
72
|
+
p 'a/b:',t['a/b'],t['a/b'].parent
|
73
|
+
puts Array.new(32,'-').join
|
74
|
+
require './exception'
|
75
|
+
p 'a/b/c:',t['a/b/c'],t['/a/b/c'].try(:parent)
|
76
|
+
puts Array.new(32,'-').join
|
77
|
+
p 'a/b/c/d:',(begin;t['a/b/c/d'];rescue(Exception);'cant pass path="a/b/c/d" with leaf["a/b/c"]';end)
|
78
|
+
puts Array.new(32,'-').join
|
79
|
+
=end
|
80
|
+
|
81
|
+
class Tree < Hash
|
82
|
+
VERSION = '0.2.0'
|
83
|
+
|
84
|
+
def self.init
|
85
|
+
Tree.new{|tree, path|tree[path] = Tree.new(tree,&tree.default_proc) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.clear
|
89
|
+
self.init
|
90
|
+
end
|
91
|
+
|
92
|
+
attr_accessor :parent
|
93
|
+
|
94
|
+
def initialize parent=nil
|
95
|
+
@parent=parent
|
96
|
+
super()
|
97
|
+
end
|
98
|
+
|
99
|
+
def [] key
|
100
|
+
unless key.instance_of?(String) && key.include?('/')
|
101
|
+
child = super(key)
|
102
|
+
child.parent = self if child.respond_to? :parent
|
103
|
+
return child
|
104
|
+
end
|
105
|
+
key.include?('/') ? send(:route,key) : super(key)
|
106
|
+
end
|
107
|
+
|
108
|
+
def []= key, value
|
109
|
+
unless key.instance_of?(String) && key.include?('/')
|
110
|
+
super(key,value)
|
111
|
+
child = self[key]
|
112
|
+
child.parent = self if child.respond_to? :parent
|
113
|
+
return child
|
114
|
+
end
|
115
|
+
key.include?('/') ? (key=='/' ? (raise PathError, "Class Tree can't use '/' as a key except rootself.") : send(:mount,key,value)) : super(key,value)
|
116
|
+
end
|
117
|
+
|
118
|
+
def route path
|
119
|
+
unless path.instance_of?(String) && path.include?('/')
|
120
|
+
post = self[path]
|
121
|
+
post.parent = self if post.respond_to? :parent
|
122
|
+
return post
|
123
|
+
end
|
124
|
+
return self if path=='/'
|
125
|
+
hops = path.split('/')
|
126
|
+
hops.delete ''
|
127
|
+
curr = hops.shift
|
128
|
+
if hops.empty?
|
129
|
+
self[curr]
|
130
|
+
else
|
131
|
+
# if a leaf exists, any path walkthrough leaf would not pass;
|
132
|
+
# if no leaf exists, any path walkthrough could be pass.
|
133
|
+
raise PathError, "The current expected path`#{path}` not exist." unless self[curr].respond_to? :route
|
134
|
+
self[curr].send "route", hops.join('/')
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Caution: #mount will arrive the deepest leaf and change the passthrough
|
139
|
+
def mount path, store
|
140
|
+
unless path.instance_of?(String) && path.include?('/')
|
141
|
+
self[path]=store
|
142
|
+
post = self[path]
|
143
|
+
post.parent = self if post.respond_to? :parent
|
144
|
+
return post
|
145
|
+
end
|
146
|
+
raise PathError, "Class Tree can't mount an value on the root." if path=='/'
|
147
|
+
hops = path.split('/')
|
148
|
+
hops.delete ''
|
149
|
+
curr = hops.shift
|
150
|
+
if hops.empty?
|
151
|
+
self[curr]=store
|
152
|
+
self.delete nil # except ['/']=store to {nil=>store}
|
153
|
+
else
|
154
|
+
self[curr]=Tree.init
|
155
|
+
# self[curr].send "route", hops.join('/')
|
156
|
+
self[curr].send "mount", hops.join('/'), store
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# Caution: #emerge can adapt on HashObject but loose the tree-ability
|
161
|
+
def emerge path, tree
|
162
|
+
if tree.is_a?(Hash)
|
163
|
+
hops = path.split("/")
|
164
|
+
popz = hops.pop
|
165
|
+
curr = hops.join('/')
|
166
|
+
target = hops.empty? ? self[popz] : self.route(curr)
|
167
|
+
raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
|
168
|
+
target.merge!(tree)
|
169
|
+
return target
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def contact path, tree
|
174
|
+
if tree.is_a?(Tree)
|
175
|
+
target = self.route path
|
176
|
+
raise PathError, "The current node [\"#{path}\"]=>#{target} not respond to contact/merge." unless target.respond_to? :route
|
177
|
+
target.merge!(tree)
|
178
|
+
tree.parent = target
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class PathError < Exception
|
183
|
+
end
|
184
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-05-12 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: custom-core is a custom library for ruby programming.
|
13
|
+
email:
|
14
|
+
- matthrewchains@gmail.com
|
15
|
+
- 18995691365@189.cn
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- LICENSE-CN.md
|
22
|
+
- README.md
|
23
|
+
- attribute.rb
|
24
|
+
- cc.rb
|
25
|
+
- chinese.rb
|
26
|
+
- chrono.rb
|
27
|
+
- enum.rb
|
28
|
+
- exception.rb
|
29
|
+
- file.rb
|
30
|
+
- kernel.rb
|
31
|
+
- monkey-patch.rb
|
32
|
+
- number.rb
|
33
|
+
- regexp.rb
|
34
|
+
- string.rb
|
35
|
+
- tree.rb
|
36
|
+
homepage: https://github.com/ChenMeng1365/custom-core
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- "."
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.6.6
|
55
|
+
specification_version: 4
|
56
|
+
summary: custom core
|
57
|
+
test_files: []
|