-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(parser): extract util functions
- Loading branch information
1 parent
261b4b2
commit 4513e9c
Showing
4 changed files
with
173 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
local M = {} | ||
|
||
---Integer in decimal form to binary string | ||
---@param input integer | ||
---@return string | ||
function M.itob(input) | ||
if input == 0 then | ||
return "0" | ||
end | ||
|
||
if input < 0 then | ||
input = math.abs(input) | ||
end | ||
|
||
local result = "" | ||
|
||
while input > 0 do | ||
local remainder = input % 2 | ||
result = remainder .. result | ||
input = math.floor(input / 2) | ||
end | ||
|
||
return result | ||
end | ||
|
||
---Checks if the string has the given number format header and returns the start of the value if it does, otherwise 1 | ||
---@param str string | ||
---@param header string[] the list of single-character format specifiers | ||
---@return integer | ||
function M.check_header(str, header) | ||
for _, ch in ipairs(header) do | ||
if str:sub(1, 1) == ch then | ||
return 2 | ||
end | ||
|
||
-- Should never occur, but just in case | ||
if str:sub(1, 1) == '0' and str:sub(2, 2) == ch then | ||
return 3 | ||
end | ||
end | ||
|
||
return 1 | ||
end | ||
|
||
-- Check whether a passed string is wrapped by quotes | ||
---@param str string | ||
---@return boolean | ||
function M.is_string_wrapped(str) | ||
return #str >= 3 and ( | ||
(str:sub(1, 1) == '"' and str:sub(str:len()) == '"') or | ||
(str:sub(1, 1) == "'" and str:sub(str:len()) == "'") | ||
) | ||
end | ||
|
||
---String in decimal form to integer | ||
---@param input string | ||
---@return integer | ||
function M.stoi(input) | ||
return tonumber(input, 10); | ||
end | ||
|
||
---String in decimal form to integer | ||
---@param input string | ||
---@return integer | ||
function M.xtoi(input) | ||
return tonumber(input, 16); | ||
end | ||
|
||
---String in decimal form to integer | ||
---@param input string | ||
---@return integer | ||
function M.btoi(input) | ||
return tonumber(input, 2); | ||
end | ||
|
||
---String in decimal form to integer | ||
---@param input string | ||
---@return integer | ||
function M.otoi(input) | ||
return tonumber(input, 8); | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
local Utils = require("hexer.parse_utils") | ||
|
||
describe("Parse Utils", function() | ||
it("should be able to detect whether a passed string is quoted", function() | ||
local strs = { | ||
["''"] = false, | ||
['""'] = false, | ||
["potato"] = false, | ||
["'p'"] = true, | ||
['"p"'] = true, | ||
["'potato'"] = true, | ||
['"potato"'] = true | ||
} | ||
|
||
local res | ||
|
||
for str, q in pairs(strs) do | ||
res = Utils.is_string_wrapped(str) == q | ||
assert(res, ("%s expected to be %s but got %s"):format(str, q, res)) | ||
end | ||
end) | ||
|
||
it("should see if a given string contains the header", function() | ||
local strs = { "0b0011", "B0011" } | ||
local headers = { { "b", "B" }, { "x", "X" } } | ||
|
||
local val_pos = Utils.check_header(strs[1], headers[1]) | ||
|
||
assert(val_pos == 3, | ||
("String %s with headers %s expected pos %s but got %s"):format(strs[1], vim.inspect(headers[1]), 3, val_pos) | ||
) | ||
|
||
val_pos = Utils.check_header(strs[1], headers[2]) | ||
|
||
assert(val_pos == 1, | ||
("String %s with headers %s expected pos %s but got %s"):format(strs[1], vim.inspect(headers[2]), 1, val_pos) | ||
) | ||
|
||
val_pos = Utils.check_header(strs[2], headers[1]) | ||
|
||
assert(val_pos == 2, | ||
("String %s with headers %s expected pos %s but got %s"):format(strs[2], vim.inspect(headers[1]), 2, val_pos) | ||
) | ||
|
||
val_pos = Utils.check_header(strs[2], headers[2]) | ||
|
||
assert(val_pos == 1, | ||
("String %s with headers %s expected pos %s but got %s"):format(strs[2], vim.inspect(headers[2]), 1, val_pos) | ||
) | ||
end) | ||
end) |
Oops, something went wrong.