Skip to content

Commit b0dee45

Browse files
authored
Merge pull request #53 from mjblack/helper_macros_and_methods_docs
Added documentation to macros file and spec tests.
2 parents 0e286fd + aacb412 commit b0dee45

3 files changed

Lines changed: 96 additions & 6 deletions

File tree

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: win32cr
2-
version: 1.2.0
2+
version: 1.2.1
33

44
authors:
55
- Matthew J. Black <mjblack@gmail.com>

spec/macros_spec.cr

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require "./spec_helper"
2+
require "../src/macros"
3+
4+
describe "Win32cr Macros" do
5+
6+
describe "pwstr" do
7+
str = "Example String"
8+
it "should take a string and create a UInt16 pointer" do
9+
pstr = pwstr(str)
10+
pstr.should be_a(Pointer(UInt16))
11+
end
12+
13+
it "should have the value of 'Example String' in UInt16 pointer" do
14+
pstr = pwstr(str)
15+
val = String.from_utf16(pstr)[0] # `from_utf16` returns a tuple, first element should be the string
16+
val.should eq(str)
17+
end
18+
end
19+
20+
describe "hiword" do
21+
value = 0x12345678
22+
pvalue = pointerof(value)
23+
24+
it "returns the high-order WORD from a UInt32" do
25+
hiword(value.to_u32).should eq(4660_u16)
26+
end
27+
28+
it "returns the high-order WORD from a Int32" do
29+
hiword(value.to_i32).should eq(4660_u16)
30+
end
31+
32+
it "returns the high-order WORD from a UInt32 pointer" do
33+
hiword(pvalue).should eq(4660_u16)
34+
end
35+
36+
it "returns the high-order WORD from a Int32 pointer" do
37+
hiword(pvalue).should eq(4660_u16)
38+
end
39+
end
40+
41+
describe "loword" do
42+
value = 0x12345678
43+
pvalue = pointerof(value)
44+
45+
it "returns the low-order WORD from a UInt32" do
46+
loword(value.to_u32).should eq(22136_u16)
47+
end
48+
49+
it "returns the low-order WORD from a Int32" do
50+
loword(value.to_i32).should eq(22136_u16)
51+
end
52+
53+
it "returns the low-order WORD from a UInt32 pointer" do
54+
loword(pvalue).should eq(22136_u16)
55+
end
56+
57+
it "returns the low-order WORD from a Int32 pointer" do
58+
loword(pvalue).should eq(22136_u16)
59+
end
60+
end
61+
end

src/macros.cr

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,55 @@
11
## Custom Macros and top level methods go here!
22
## WinMD will not overwrite this file.
33

4+
# Converts a string to a pointer to a null-terminated wide string pointer.
45
macro pwstr(str)
56
{{str}}.to_utf16.to_unsafe
67
end
78

8-
def loword(i : UInt64) : UInt16
9+
# Returns the low-order `WORD` from a `UInt64`.
10+
def loword(i : UInt32) : UInt16
911
val = (i & 0xffff).to_u16
1012
val
1113
end
1214

13-
def loword(i : Int64) : UInt16
14-
val = (i.to_u64 & 0xffff).to_u16
15+
# Returns the low-order `WORD` from a `Int64`.
16+
def loword(i : Int32) : UInt16
17+
val = (i.to_u32 & 0xffff).to_u16
1518
val
1619
end
1720

21+
# Returns the low-order `WORD` from a `UInt64` pointer.
22+
def loword(i : UInt32*) : UInt16
23+
val = (i.value & 0xffff).to_u16
24+
val
25+
end
26+
27+
# Returns the low-order `WORD` from a `Int64` pointer.
28+
def loword(i : Int32*) : UInt16
29+
val = (i.value.to_u32 & 0xffff).to_u16
30+
val
31+
end
32+
33+
# Returns the high-order `WORD` from a `UInt32`.
34+
def hiword(i : UInt32) : UInt16
35+
val = ((i >> 16) & 0xFFFF).to_u16
36+
val
37+
end
38+
39+
# Returns the high-order `WORD` from a `Int32`.
40+
def hiword(i : Int32) : UInt16
41+
val = ((i.to_u32 >> 16) & 0xFFFF).to_u16
42+
val
43+
end
44+
45+
# Returns the high-order `WORD` from a `UInt32` pointer.
1846
def hiword(i : UInt32*) : UInt16
1947
val = ((i.value >> 16) & 0xFFFF).to_u16
2048
val
2149
end
2250

23-
def hiword(i : Int64*) : UInt16
24-
val = ((i.value.to_u64 >> 16) & 0xFFFF).to_u16
51+
# Returns the high-order `WORD` from a `Int32` pointer.
52+
def hiword(i : Int32*) : UInt16
53+
val = ((i.value.to_u32 >> 16) & 0xFFFF).to_u16
2554
val
2655
end

0 commit comments

Comments
 (0)