libpath-ruby 0.2.1

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.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +61 -0
  3. data/examples/path_from_arg0.md +99 -0
  4. data/examples/path_from_arg0.rb +55 -0
  5. data/lib/libpath.rb +12 -0
  6. data/lib/libpath/constants.rb +42 -0
  7. data/lib/libpath/constants/unix.rb +146 -0
  8. data/lib/libpath/constants/windows.rb +147 -0
  9. data/lib/libpath/diagnostics.rb +6 -0
  10. data/lib/libpath/diagnostics/parameter_checking.rb +46 -0
  11. data/lib/libpath/exceptions.rb +7 -0
  12. data/lib/libpath/exceptions/libpath_base_exception.rb +77 -0
  13. data/lib/libpath/exceptions/malformed_name_exception.rb +85 -0
  14. data/lib/libpath/form.rb +42 -0
  15. data/lib/libpath/form/unix.rb +215 -0
  16. data/lib/libpath/form/windows.rb +358 -0
  17. data/lib/libpath/internal_/array.rb +96 -0
  18. data/lib/libpath/internal_/platform.rb +53 -0
  19. data/lib/libpath/internal_/string.rb +33 -0
  20. data/lib/libpath/internal_/unix/form.rb +160 -0
  21. data/lib/libpath/internal_/windows/drive.rb +84 -0
  22. data/lib/libpath/internal_/windows/form.rb +281 -0
  23. data/lib/libpath/libpath.rb +6 -0
  24. data/lib/libpath/path.rb +43 -0
  25. data/lib/libpath/path/unix.rb +170 -0
  26. data/lib/libpath/path/windows.rb +176 -0
  27. data/lib/libpath/util.rb +42 -0
  28. data/lib/libpath/util/unix.rb +414 -0
  29. data/lib/libpath/util/windows.rb +636 -0
  30. data/lib/libpath/version.rb +73 -0
  31. data/test/performance/benchmark_drive_letter.rb +31 -0
  32. data/test/performance/benchmark_gsub_string_or_regex.rb +45 -0
  33. data/test/performance/benchmark_rindex2.rb +109 -0
  34. data/test/performance/benchmark_split.rb +32 -0
  35. data/test/unit/compare/ts_all.rb +22 -0
  36. data/test/unit/equate/ts_all.rb +22 -0
  37. data/test/unit/equate/unix/ts_all.rb +22 -0
  38. data/test/unit/equate/windows/ts_all.rb +22 -0
  39. data/test/unit/exceptions/tc_libpath_base_exception.rb +27 -0
  40. data/test/unit/exceptions/tc_malformed_name_exception.rb +31 -0
  41. data/test/unit/exceptions/ts_all.rb +22 -0
  42. data/test/unit/form/tc_absolute_functions.rb +369 -0
  43. data/test/unit/form/ts_all.rb +22 -0
  44. data/test/unit/form/unix/tc_absolute_functions.rb +269 -0
  45. data/test/unit/form/unix/ts_all.rb +22 -0
  46. data/test/unit/form/windows/tc_absolute_functions.rb +854 -0
  47. data/test/unit/form/windows/ts_all.rb +22 -0
  48. data/test/unit/internal_/tc_array.rb +62 -0
  49. data/test/unit/internal_/ts_all.rb +22 -0
  50. data/test/unit/internal_/unix/form/tc_slash_functions.rb +60 -0
  51. data/test/unit/internal_/unix/form/ts_all.rb +22 -0
  52. data/test/unit/internal_/unix/tc_split_path.rb +396 -0
  53. data/test/unit/internal_/unix/ts_all.rb +22 -0
  54. data/test/unit/internal_/windows/form/tc_get_windows_volume.rb +220 -0
  55. data/test/unit/internal_/windows/form/tc_slash_functions.rb +61 -0
  56. data/test/unit/internal_/windows/form/ts_all.rb +22 -0
  57. data/test/unit/internal_/windows/tc_split_path.rb +881 -0
  58. data/test/unit/internal_/windows/ts_all.rb +22 -0
  59. data/test/unit/parse/ts_all.rb +22 -0
  60. data/test/unit/path/tc_path.rb +778 -0
  61. data/test/unit/path/ts_all.rb +22 -0
  62. data/test/unit/path/unix/tc_path.rb +565 -0
  63. data/test/unit/path/unix/ts_all.rb +22 -0
  64. data/test/unit/path/windows/tc_path.rb +630 -0
  65. data/test/unit/path/windows/ts_all.rb +22 -0
  66. data/test/unit/tc_version.rb +47 -0
  67. data/test/unit/ts_all.rb +22 -0
  68. data/test/unit/util/tc_combine_paths.rb +179 -0
  69. data/test/unit/util/tc_derive_relative_path.rb +19 -0
  70. data/test/unit/util/tc_make_path_canonical.rb +228 -0
  71. data/test/unit/util/ts_all.rb +22 -0
  72. data/test/unit/util/unix/tc_combine_paths.rb +65 -0
  73. data/test/unit/util/unix/tc_derive_relative_path.rb +123 -0
  74. data/test/unit/util/unix/tc_make_path_absolute.rb +117 -0
  75. data/test/unit/util/unix/tc_make_path_canonical.rb +139 -0
  76. data/test/unit/util/unix/ts_all.rb +22 -0
  77. data/test/unit/util/windows/tc_combine_paths.rb +131 -0
  78. data/test/unit/util/windows/tc_derive_relative_path.rb +155 -0
  79. data/test/unit/util/windows/tc_make_path_absolute.rb +163 -0
  80. data/test/unit/util/windows/tc_make_path_canonical.rb +220 -0
  81. data/test/unit/util/windows/ts_all.rb +22 -0
  82. metadata +144 -0
@@ -0,0 +1,6 @@
1
+
2
+ require 'libpath/diagnostics/parameter_checking'
3
+
4
+ # ############################## end of file ############################# #
5
+
6
+
@@ -0,0 +1,46 @@
1
+
2
+ module LibPath # :nodoc:
3
+ module Diagnostics # :nodoc:
4
+
5
+ # @!visibility private
6
+ def self.check_string_parameter param_value, param_name, **options # :nodoc:
7
+
8
+ case param_value
9
+ when nil
10
+
11
+ unless options[:allow_nil]
12
+
13
+ raise ::ArgumentError, "parameter '#{param_name}' may not be nil"
14
+ end
15
+
16
+ ;
17
+ when ::String
18
+
19
+ ;
20
+ else
21
+
22
+ unless param_value.respond_to?(:to_str)
23
+
24
+ raise ::TypeError, "parameter '#{param_name}' must be instance of #{::String} or respond to #to_str()"
25
+ end
26
+ end
27
+ end
28
+
29
+ # @!visibility private
30
+ def self.check_options h, *args, **options # :nodoc:
31
+
32
+ if known = options[:known] then
33
+
34
+ h.each_key do |k|
35
+
36
+ raise ::ArgumentError, "unknown key '#{k}'" unless known.include?(k)
37
+ end
38
+ end
39
+
40
+ end
41
+ end # module Diagnostics
42
+ end # module LibPath
43
+
44
+ # ############################## end of file ############################# #
45
+
46
+
@@ -0,0 +1,7 @@
1
+
2
+ require 'libpath/excdeptions/libpath_base_exception'
3
+ require 'libpath/excdeptions/malformed_name_exception'
4
+
5
+ # ############################## end of file ############################# #
6
+
7
+
@@ -0,0 +1,77 @@
1
+
2
+ # ######################################################################## #
3
+ # File: libpath/exceptions/libpath_base_exception.rb
4
+ #
5
+ # Purpose: LibPathBaseException class
6
+ #
7
+ # Created: 30th January 2019
8
+ # Updated: 16th April 2019
9
+ #
10
+ # Home: http://github.com/synesissoftware/libpath.Ruby
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 2019, Matthew Wilson and Synesis Software
15
+ # All rights reserved.
16
+ #
17
+ # Redistribution and use in source and binary forms, with or without
18
+ # modification, are permitted provided that the following conditions are
19
+ # met:
20
+ #
21
+ # * Redistributions of source code must retain the above copyright
22
+ # notice, this list of conditions and the following disclaimer.
23
+ #
24
+ # * Redistributions in binary form must reproduce the above copyright
25
+ # notice, this list of conditions and the following disclaimer in the
26
+ # documentation and/or other materials provided with the distribution.
27
+ #
28
+ # * Neither the names of the copyright holder nor the names of its
29
+ # contributors may be used to endorse or promote products derived from
30
+ # this software without specific prior written permission.
31
+ #
32
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
+ #
44
+ # ######################################################################## #
45
+
46
+
47
+
48
+ =begin
49
+ =end
50
+
51
+ module LibPath # :nodoc:
52
+ module Exceptions # :nodoc:
53
+
54
+ # Root exception for all LibPath exceptions
55
+ #
56
+ # NOTE: this class is abstract
57
+ class LibPathBaseException < StandardError
58
+
59
+ =begin
60
+
61
+ # TODO include Xqsr3's WithCause
62
+ =end
63
+
64
+ def self.new *args
65
+
66
+ raise NoMethodError, "private method `new' called for #{self}:Class" if self == LibPathBaseException
67
+
68
+ super
69
+ end
70
+ end
71
+
72
+ end # module Exceptions
73
+ end # module LibPath
74
+
75
+ # ############################## end of file ############################# #
76
+
77
+
@@ -0,0 +1,85 @@
1
+
2
+ # ######################################################################## #
3
+ # File: libpath/exceptions/malformed_name_exception.rb
4
+ #
5
+ # Purpose: MalformedNameException class
6
+ #
7
+ # Created: 30th January 2019
8
+ # Updated: 16th April 2019
9
+ #
10
+ # Home: http://github.com/synesissoftware/libpath.Ruby
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 2019, Matthew Wilson and Synesis Software
15
+ # All rights reserved.
16
+ #
17
+ # Redistribution and use in source and binary forms, with or without
18
+ # modification, are permitted provided that the following conditions are
19
+ # met:
20
+ #
21
+ # * Redistributions of source code must retain the above copyright
22
+ # notice, this list of conditions and the following disclaimer.
23
+ #
24
+ # * Redistributions in binary form must reproduce the above copyright
25
+ # notice, this list of conditions and the following disclaimer in the
26
+ # documentation and/or other materials provided with the distribution.
27
+ #
28
+ # * Neither the names of the copyright holder nor the names of its
29
+ # contributors may be used to endorse or promote products derived from
30
+ # this software without specific prior written permission.
31
+ #
32
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
+ #
44
+ # ######################################################################## #
45
+
46
+
47
+
48
+ =begin
49
+ =end
50
+
51
+ require 'libpath/exceptions/libpath_base_exception'
52
+
53
+ module LibPath # :nodoc:
54
+ module Exceptions # :nodoc:
55
+
56
+ # Exception thrown when a malformed name is encountered
57
+ class MalformedNameException < LibPathBaseException
58
+
59
+ # Initialises an instance from the given +name+ and optional +message+
60
+ #
61
+ # * *Parameters:*
62
+ # - +name+ (String) The name that is malformed
63
+ # - +message+ (String) {optional} The message. If +nil+ the message "malformed name '#{name}'" is used
64
+ def initialize name, message = nil
65
+
66
+ unless message
67
+
68
+ message = "malformed name '#{name}'"
69
+ end
70
+
71
+ super message
72
+
73
+ @name = name
74
+ end
75
+
76
+ # (String) The name that is malformed
77
+ attr_reader :name
78
+ end
79
+
80
+ end # module Exceptions
81
+ end # module LibPath
82
+
83
+ # ############################## end of file ############################# #
84
+
85
+
@@ -0,0 +1,42 @@
1
+
2
+ require 'libpath/internal_/platform'
3
+
4
+ if ::LibPath::Internal_::Platform::Constants::PLATFORM_IS_WINDOWS then
5
+
6
+ require 'libpath/form/windows'
7
+ else
8
+
9
+ require 'libpath/form/unix'
10
+ end
11
+
12
+ module LibPath # :nodoc:
13
+ module Form # :nodoc:
14
+
15
+ if ::LibPath::Internal_::Platform::Constants::PLATFORM_IS_WINDOWS then
16
+
17
+ extend ::LibPath::Form::Windows
18
+ include ::LibPath::Form::Windows
19
+ else
20
+
21
+ extend ::LibPath::Form::Unix
22
+ include ::LibPath::Form::Unix
23
+ end
24
+
25
+ # @!visibility private
26
+ def self.extended receiver # :nodoc:
27
+
28
+ $stderr.puts "#{receiver} extended by #{self}" if $DEBUG
29
+ end
30
+
31
+ # @!visibility private
32
+ def self.included receiver # :nodoc:
33
+
34
+ $stderr.puts "#{receiver} included #{self}" if $DEBUG
35
+ end
36
+
37
+ end # module Form
38
+ end # module LibPath
39
+
40
+ # ############################## end of file ############################# #
41
+
42
+
@@ -0,0 +1,215 @@
1
+
2
+ # ######################################################################## #
3
+ # File: libpath/form/unix.rb
4
+ #
5
+ # Purpose: LibPath::Form::Unix module
6
+ #
7
+ # Created: 8th January 2019
8
+ # Updated: 16th April 2019
9
+ #
10
+ # Home: http://github.com/synesissoftware/libpath.Ruby
11
+ #
12
+ # Author: Matthew Wilson
13
+ #
14
+ # Copyright (c) 2019, Matthew Wilson and Synesis Software
15
+ # All rights reserved.
16
+ #
17
+ # Redistribution and use in source and binary forms, with or without
18
+ # modification, are permitted provided that the following conditions are
19
+ # met:
20
+ #
21
+ # * Redistributions of source code must retain the above copyright
22
+ # notice, this list of conditions and the following disclaimer.
23
+ #
24
+ # * Redistributions in binary form must reproduce the above copyright
25
+ # notice, this list of conditions and the following disclaimer in the
26
+ # documentation and/or other materials provided with the distribution.
27
+ #
28
+ # * Neither the names of the copyright holder nor the names of its
29
+ # contributors may be used to endorse or promote products derived from
30
+ # this software without specific prior written permission.
31
+ #
32
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
+ #
44
+ # ######################################################################## #
45
+
46
+
47
+
48
+ =begin
49
+ =end
50
+
51
+ require 'libpath/constants/unix'
52
+ require 'libpath/diagnostics'
53
+
54
+ module LibPath # :nodoc:
55
+ module Form # :nodoc:
56
+ module Unix # :nodoc:
57
+
58
+ # Module defining instance functions that will be included and extended into
59
+ # any class or module including/extending module LibPath::Form::Unix
60
+ module LibPath_Form_Unix_Methods
61
+
62
+ # Classifies a path
63
+ #
64
+ # === Return
65
+ #
66
+ # One of +:absolute+, +:homed+, +:relative+, for
67
+ # any paths that match precisely those classifications, or +nil+ if the
68
+ # path is empty
69
+ def classify_path path
70
+
71
+ Diagnostics.check_string_parameter(path, "path") if $DEBUG
72
+
73
+ return nil if path.nil? || path.empty?
74
+
75
+ return :homed if path_is_homed? path
76
+
77
+ return :absolute if path_is_absolute? path
78
+
79
+ :relative
80
+ end
81
+
82
+ # Evaluates whether the given name is malformed
83
+ #
84
+ # === Signature
85
+ #
86
+ # * *Options:*
87
+ # - +:reject_path_name_separators+:: (boolean) Reject the path
88
+ # separator character(s): +'/'+
89
+ # - +:reject_path_separators+:: (boolean) Reject the path separator
90
+ # character(s): +':'+
91
+ # - +:reject_shell_characters+:: (boolean) Reject the shell
92
+ # character(s): +'*'+, +'?'+, +'|'+
93
+ def name_is_malformed? name, **options
94
+
95
+ _Constants = ::LibPath::Constants::Unix
96
+
97
+ if name
98
+
99
+ if options[:reject_path_name_separators]
100
+
101
+ return true if name =~ _Constants::InvalidCharacters::PathNameSeparators::RE
102
+ end
103
+
104
+ if options[:reject_path_separators]
105
+
106
+ return true if name =~ _Constants::InvalidCharacters::PathSeparators::RE
107
+ end
108
+
109
+ if options[:reject_shell_characters]
110
+
111
+ return true if name =~ _Constants::InvalidCharacters::Shell::RE
112
+ end
113
+
114
+ return true if name =~ _Constants::InvalidCharacters::Innate::RE
115
+
116
+ false
117
+ else
118
+
119
+ true
120
+ end
121
+ end
122
+
123
+ # Evaluates whether the given path is absolute, which means it is either
124
+ # rooted (begins with '/') or is homed (is '~' or begins with '~/')
125
+ #
126
+ # === Signature
127
+ #
128
+ # * *Parameters:*
129
+ # - +path+:: (String) The path to be evaluated. May not be +nil+
130
+ def path_is_absolute? path
131
+
132
+ Diagnostics.check_string_parameter(path, "path") if $DEBUG
133
+
134
+ case path[0]
135
+ when '/'
136
+
137
+ true
138
+ when '~'
139
+
140
+ 1 == path.size || '/' == path[1]
141
+ else
142
+
143
+ false
144
+ end
145
+ end
146
+
147
+ # Evaluates whether the given path is homed, which means it is '~' or
148
+ # begins with '~/'
149
+ #
150
+ # === Signature
151
+ #
152
+ # * *Parameters:*
153
+ # - +path+:: (String) The path to be evaluated. May not be +nil+
154
+ def path_is_homed? path
155
+
156
+ Diagnostics.check_string_parameter(path, "path") if $DEBUG
157
+
158
+ return false unless '~' == path[0]
159
+
160
+ if path.size > 1
161
+
162
+ return '/' == path[1]
163
+ end
164
+
165
+ true
166
+ end
167
+
168
+ # Evalutes whether the given path is rooted, which means it begins with
169
+ # '/'
170
+ #
171
+ # === Signature
172
+ #
173
+ # * *Parameters:*
174
+ # - +path+:: (String) The path to be evaluated. May not be +nil+
175
+ def path_is_rooted? path
176
+
177
+ Diagnostics.check_string_parameter(path, "path") if $DEBUG
178
+
179
+ '/' == path[0]
180
+ end
181
+
182
+ end # module LibPath_Form_Unix_Methods
183
+
184
+ # @!visibility private
185
+ def self.extended receiver # :nodoc:
186
+
187
+ receiver.class_eval do
188
+
189
+ extend LibPath_Form_Unix_Methods
190
+ end
191
+
192
+ $stderr.puts "#{receiver} extended by #{LibPath_Form_Unix_Methods}" if $DEBUG
193
+ end
194
+
195
+ # @!visibility private
196
+ def self.included receiver # :nodoc:
197
+
198
+ receiver.class_eval do
199
+
200
+ include LibPath_Form_Unix_Methods
201
+ end
202
+
203
+ $stderr.puts "#{receiver} included #{LibPath_Form_Unix_Methods}" if $DEBUG
204
+ end
205
+
206
+ extend LibPath_Form_Unix_Methods
207
+ include LibPath_Form_Unix_Methods
208
+
209
+ end # module Unix
210
+ end # module Form
211
+ end # module LibPath
212
+
213
+ # ############################## end of file ############################# #
214
+
215
+