Skip to content

Commit 5abb62c

Browse files
Add XML.libxml2_version (#15623)
As discussed in #13273, we're missing a consistent format for exposing version information. Returning the version number formatted as a string seems the most expected variant, though. We might consider adding extra information as well, but I'm not sure about that.
1 parent 8a857ac commit 5abb62c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

spec/std/xml/xml_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,8 @@ describe XML do
609609
end.should eq %[<foo><bar/></foo>\n]
610610
end
611611
end
612+
613+
it ".libxml2_version" do
614+
XML.libxml2_version.should match /2\.\d+\.\d+/
615+
end
612616
end

src/xml.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ module XML
125125
ptr.value = old
126126
end
127127
end
128+
129+
class_getter libxml2_version : String do
130+
version_string = String.new(LibXML.xmlParserVersion)
131+
132+
# The version string can contain extra information after the version number,
133+
# so we ignore any trailing non-numbers with `strict: false`
134+
number = version_string.to_i(strict: false)
135+
136+
# Construct a formatted version string
137+
"#{number // 10_000}.#{number % 10_000 // 100}.#{number % 100}"
138+
end
128139
end
129140

130141
require "./xml/*"

src/xml/libxml2.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ require "./save_options"
1919
lib LibXML
2020
alias Int = LibC::Int
2121

22+
$xmlParserVersion : LibC::Char*
23+
2224
fun xmlInitParser
2325

2426
fun __xmlIndentTreeOutput : Int*

0 commit comments

Comments
 (0)