Skip to content

Commit

Permalink
Fix using tonumber on floats, add tostringview function
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Aug 15, 2020
1 parent 1a06a2b commit 47ca24b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/allocators/interface.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require 'span'
local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end

## Allocator.value.is_allocator = true

local is_span = #[concept(function(x) return x.type.is_span end)]#
local Allocator = #[Allocator]#

Expand Down
19 changes: 17 additions & 2 deletions lib/stringview.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ function stringview.find(self: stringview,
return 0, 0
end

-- Convert a type to a string view.
global function tostringview(x: auto): stringview
## if x.type.is_stringview then
return x
## elseif x.type.is_record and x.type:get_metafield('__tostringview') then
return x:__tostringview()
## else
staticerror("tostringview: cannot convert type '%s' to a string", x.type)
## end
end

-- Convert a string to an integer in the desired base.
local function str2intbase(s: stringview, base: uinteger): integer
assert(s.size > 0, "str2intbase: invalid number format")
Expand Down Expand Up @@ -164,9 +175,12 @@ global function tonumber(x: auto, base: #[optional_concept(integer)]#)
## if not base.type.is_niltype then
## staticassert(x.type.is_stringy, "string expected, got something else")
return str2intbase(x, base)
## elseif x.type.is_arithmetic then
## elseif x.type.is_integral then
local n: integer = x
return n
## elseif x.type.is_float then
local n: number = x
return n
## elseif x.type.is_stringy then
local x: cstring = x
local endptr: cstring
Expand All @@ -183,7 +197,8 @@ global function tointeger(x: auto, base: #[optional_concept(integer)]#): integer
## staticassert(x.type.is_stringy, "string expected, got something else")
return str2intbase(x, base)
## elseif x.type.is_arithmetic then
return x
local n: integer = x
return n
## elseif x.type.is_stringy then
local x: cstring = x
local endptr: cstring
Expand Down
5 changes: 4 additions & 1 deletion rockspecs/nelua-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ build = {
['lib/allocators/general.nelua'] = 'lib/allocators/general.nelua',
['lib/allocators/gc.nelua'] = 'lib/allocators/gc.nelua',
['lib/allocators/arena.nelua'] = 'lib/allocators/arena.nelua',
['lib/allocators/stack.nelua'] = 'lib/allocators/stack.nelua',
['lib/allocators/heap.nelua'] = 'lib/allocators/heap.nelua',
['lib/allocators/pool.nelua'] = 'lib/allocators/pool.nelua',
['lib/basic.nelua'] = 'lib/basic.nelua',
['lib/io.nelua'] = 'lib/io.nelua',
['lib/math.nelua'] = 'lib/math.nelua',
Expand All @@ -110,7 +113,7 @@ build = {
['lib/vector.nelua'] = 'lib/vector.nelua',
['lib/string.nelua'] = 'lib/string.nelua',
['lib/stringview.nelua'] = 'lib/stringview.nelua',
['lib/stringbuilder.nelua'] = 'lib/stringbuilder.nelua',
['lib/stringbuilder.nelua'] = 'lib/stringbuilder.nelua',
['lib/resourcepool.nelua'] = 'lib/resourcepool.nelua',
['lib/filestream.nelua'] = 'lib/filestream.nelua',
['lib/table.nelua'] = 'lib/table.nelua',
Expand Down
8 changes: 8 additions & 0 deletions tests/string_test.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ do -- tostring
assert(tostring(tostring) == 'lazyfunction')
end

do -- tostringview
assert(tostringview('a') == 'a')
local s: string = 'test'
assert(tostringview(s) == 'test')
end

do -- string implicit conversion
local v: stringview
local s: string
Expand All @@ -325,6 +331,8 @@ do -- tonumber
assert(tonumber('1337') == 1337)
assert(tonumber(0) == 0)
assert(tonumber('0') == 0)
assert(tonumber(3.4) == 3.4)
assert(tonumber(1/0) == 1/0)

local v: stringview = '1337'
assert(tonumber(v) == 1337)
Expand Down

0 comments on commit 47ca24b

Please sign in to comment.