Skip to content

Commit c0e7620

Browse files
committed
Version string parsing and comparison functions
1 parent 732cc21 commit c0e7620

File tree

5 files changed

+116
-7
lines changed

5 files changed

+116
-7
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ from the source code of the miscellaneous scripts using the Python module
3737

3838
<!-- Start of generated documentation -->
3939

40-
The documentation of the 68 functions below was extracted from
41-
15 Vim scripts on June 19, 2013 at 22:53.
40+
The documentation of the 72 functions below was extracted from
41+
16 Vim scripts on June 22, 2013 at 02:08.
4242

4343
### Handling of special buffers
4444

@@ -570,6 +570,14 @@ Test synchronous command execution without raising of errors with
570570
Test basic functionality of asynchronous command execution with
571571
`xolox#misc#os#exec()`.
572572

573+
#### The `xolox#misc#tests#version_string_parsing()` function
574+
575+
Test parsing of version strings with `xolox#misc#version#parse()`.
576+
577+
#### The `xolox#misc#tests#version_string_comparison()` function
578+
579+
Test comparison of version strings with `xolox#misc#version#at_least()`.
580+
573581
### Timing of long during operations
574582

575583
#### The `xolox#misc#timer#start()` function
@@ -598,6 +606,17 @@ handling as Vim's [printf()] [printf] function with one difference: At the
598606
point where you want the elapsed time to be embedded, you write `%s` and
599607
you pass the list returned by `xolox#misc#timer#start()` as an argument.
600608

609+
### Version string handling
610+
611+
#### The `xolox#misc#version#parse()` function
612+
613+
Convert a version string to a list of integers.
614+
615+
#### The `xolox#misc#version#at_least()` function
616+
617+
Check whether the second version string is equal to or greater than the
618+
first version string. Returns 1 (true) when it is, 0 (false) otherwise.
619+
601620
<!-- End of generated documentation -->
602621

603622
## Contact

autoload/xolox/misc.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" The version of my miscellaneous scripts.
22
"
33
" Author: Peter Odding <[email protected]>
4-
" Last Change: June 19, 2013
4+
" Last Change: June 22, 2013
55
" URL: http://peterodding.com/code/vim/misc/
66

7-
let g:xolox#misc#version = '1.6.3'
7+
let g:xolox#misc#version = '1.7'

autoload/xolox/misc/tests.vim

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Tests for the miscellaneous Vim scripts.
22
"
33
" Author: Peter Odding <[email protected]>
4-
" Last Change: June 2, 2013
4+
" Last Change: June 22, 2013
55
" URL: http://peterodding.com/code/vim/misc/
66
"
77
" The Vim auto-load script `autoload/xolox/misc/tests.vim` contains the
@@ -17,6 +17,7 @@ function! xolox#misc#tests#run() " {{{1
1717
call s:test_list_handling()
1818
call s:test_option_handling()
1919
call s:test_command_execution()
20+
call s:test_version_handling()
2021
" Report a short summary to the user.
2122
call xolox#misc#test#summarize()
2223
endfunction
@@ -228,3 +229,28 @@ function! xolox#misc#tests#asynchronous_command_execution() " {{{2
228229
call xolox#misc#test#assert_true(filereadable(tempfile))
229230
call xolox#misc#test#assert_equals([expected_value], readfile(tempfile))
230231
endfunction
232+
233+
" Tests for autoload/xolox/misc/version.vim {{{1
234+
235+
function! s:test_version_handling()
236+
call xolox#misc#test#wrap('xolox#misc#tests#version_string_parsing')
237+
call xolox#misc#test#wrap('xolox#misc#tests#version_string_comparison')
238+
endfunction
239+
240+
function! xolox#misc#tests#version_string_parsing() " {{{2
241+
" Test parsing of version strings with `xolox#misc#version#parse()`.
242+
call xolox#misc#test#assert_equals([1], xolox#misc#version#parse('1'))
243+
call xolox#misc#test#assert_equals([1, 5], xolox#misc#version#parse('1.5'))
244+
call xolox#misc#test#assert_equals([1, 22, 3333, 44444, 55555], xolox#misc#version#parse('1.22.3333.44444.55555'))
245+
call xolox#misc#test#assert_equals([1, 5], xolox#misc#version#parse('1x.5y'))
246+
endfunction
247+
248+
function! xolox#misc#tests#version_string_comparison() " {{{2
249+
" Test comparison of version strings with `xolox#misc#version#at_least()`.
250+
call xolox#misc#test#assert_true(xolox#misc#version#at_least('1', '1'))
251+
call xolox#misc#test#assert_true(!xolox#misc#version#at_least('1', '0'))
252+
call xolox#misc#test#assert_true(xolox#misc#version#at_least('1', '2'))
253+
call xolox#misc#test#assert_true(xolox#misc#version#at_least('1.2.3', '1.2.3'))
254+
call xolox#misc#test#assert_true(!xolox#misc#version#at_least('1.2.3', '1.2'))
255+
call xolox#misc#test#assert_true(xolox#misc#version#at_least('1.2.3', '1.2.4'))
256+
endfunction

autoload/xolox/misc/version.vim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
" Version string handling.
2+
"
3+
" Author: Peter Odding <[email protected]>
4+
" Last Change: June 22, 2013
5+
" URL: http://peterodding.com/code/vim/misc/
6+
7+
function! xolox#misc#version#parse(version_string)
8+
" Convert a version string to a list of integers.
9+
let result = map(split(a:version_string, '\.'), 'v:val + 0')
10+
call xolox#misc#msg#debug("vim-misc %s: Parsed version string %s into %s.", g:xolox#misc#version, string(a:version_string), string(result))
11+
return result
12+
endfunction
13+
14+
function! xolox#misc#version#at_least(expected_version, available_version)
15+
" Check whether the second version string is equal to or greater than the
16+
" first version string. Returns 1 (true) when it is, 0 (false) otherwise.
17+
let expected_version = xolox#misc#version#parse(a:expected_version)
18+
let available_version = xolox#misc#version#parse(a:available_version)
19+
for idx in range(max([len(expected_version), len(available_version)]))
20+
let expected_number = get(expected_version, idx, 0)
21+
let available_number = get(available_version, idx, 0)
22+
if available_number > expected_number
23+
call xolox#misc#msg#debug("vim-misc %s: Available version (%s) is higher than expected version (%s).", g:xolox#misc#version, a:available_version, a:expected_version)
24+
return 1
25+
elseif available_number < expected_number
26+
call xolox#misc#msg#debug("vim-misc %s: Available version (%s) is lower than expected version (%s).", g:xolox#misc#version, a:available_version, a:expected_version)
27+
return 0
28+
endif
29+
endfor
30+
call xolox#misc#msg#debug("vim-misc %s: Available version (%s) is equal to expected version (%s).", g:xolox#misc#version, a:available_version, a:expected_version)
31+
return 1
32+
endfunction
33+
34+
" vim: ts=2 sw=2 et

doc/misc.txt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ function
8686
14. The |xolox#misc#tests#synchronous_command_execution_without_raising_errors()|
8787
function
8888
15. The |xolox#misc#tests#asynchronous_command_execution()| function
89+
16. The |xolox#misc#tests#version_string_parsing()| function
90+
17. The |xolox#misc#tests#version_string_comparison()| function
8991
14. Timing of long during operations |misc-timing-of-long-during-operations|
9092
1. The |xolox#misc#timer#start()| function
9193
2. The |xolox#misc#timer#stop()| function
9294
3. The |xolox#misc#timer#force()| function
95+
15. Version string handling |misc-version-string-handling|
96+
1. The |xolox#misc#version#parse()| function
97+
2. The |xolox#misc#version#at_least()| function
9398
4. Contact |misc-contact|
9499
5. License |misc-license|
95100
6. References |misc-references|
@@ -137,8 +142,8 @@ For those who are curious: The function descriptions given below were extracted
137142
from the source code of the miscellaneous scripts using the Python module
138143
'vimdoctool.py' included in vim-tools [5].
139144

140-
The documentation of the 68 functions below was extracted from 15 Vim scripts
141-
on June 19, 2013 at 22:53.
145+
The documentation of the 72 functions below was extracted from 16 Vim scripts
146+
on June 22, 2013 at 02:08.
142147

143148
-------------------------------------------------------------------------------
144149
*misc-handling-of-special-buffers*
@@ -723,6 +728,16 @@ The *xolox#misc#tests#asynchronous_command_execution()* function
723728
Test basic functionality of asynchronous command execution with
724729
|xolox#misc#os#exec()|.
725730

731+
-------------------------------------------------------------------------------
732+
The *xolox#misc#tests#version_string_parsing()* function
733+
734+
Test parsing of version strings with |xolox#misc#version#parse()|.
735+
736+
-------------------------------------------------------------------------------
737+
The *xolox#misc#tests#version_string_comparison()* function
738+
739+
Test comparison of version strings with |xolox#misc#version#at_least()|.
740+
726741
-------------------------------------------------------------------------------
727742
*misc-timing-of-long-during-operations*
728743
Timing of long during operations ~
@@ -752,6 +767,21 @@ handling as Vim's |printf()| function with one difference: At the point where
752767
you want the elapsed time to be embedded, you write '%s' and you pass the list
753768
returned by |xolox#misc#timer#start()| as an argument.
754769

770+
-------------------------------------------------------------------------------
771+
*misc-version-string-handling*
772+
Version string handling ~
773+
774+
-------------------------------------------------------------------------------
775+
The *xolox#misc#version#parse()* function
776+
777+
Convert a version string to a list of integers.
778+
779+
-------------------------------------------------------------------------------
780+
The *xolox#misc#version#at_least()* function
781+
782+
Check whether the second version string is equal to or greater than the first
783+
version string. Returns 1 (true) when it is, 0 (false) otherwise.
784+
755785
===============================================================================
756786
*misc-contact*
757787
Contact ~

0 commit comments

Comments
 (0)