string to int


See On Github

Data

Contributor

Generic placeholder thumbnail

by Yonaba

in lua

Tags

conversion

Source Code

-- Converts a string to an integer
--  without using any standard library function

-- Returns (base ^ exp)
local function power(base, exp)
  if exp <= 0 or not exp then return 1 end
  local pow = base
  for i = 1, exp-1 do pow = pow * base end
  return pow
end

-- str    : a string to be converted to an int
-- returns: an integer if the conversion is possible
--   otherwise, it errs
return function(str)
  local len = #str
  local exp, num = len , 0
  assert(not str:match('[^%d]'), 'Not an integer')
  for digit in str:gmatch('.') do
    exp = exp - 1
    num = num + (digit) * power(10, exp)
  end
  return num
end
-- Tests for string_to_int.lua
local tonumber = require 'string_to_int'

local total, pass = 0, 0

local function dec(str, len)
  return #str < len
     and str .. (('.'):rep(len-#str))
      or str:sub(1,len)
end

local function run(message, f)
  total = total + 1
  local ok, err = pcall(f)
  if ok then pass = pass + 1 end
  local status = ok and 'PASSED' or 'FAILED'
  print(('%02d. %68s: %s'):format(total, dec(message,68), status))
end


run('Fails if not given an integer as a string', function()
  assert(not pcall(tonumber,  '-1'))
  assert(not pcall(tonumber, '1.5'))
  assert(not pcall(tonumber,     1))
end)

run('Converts string to int', function()
  assert(tonumber('1')   ==   1)
  assert(tonumber('548') == 548)
  assert(tonumber('002') ==   2)
  assert(tonumber('082') ==  82)
  assert(tonumber('0')   ==   0)
end)

print(('-'):rep(80))
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
  :format(total, pass, total-pass, (pass*100/total)))