login

<     >

2023-10-09 14:14:00 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

shp: correctly trim nul-terminated DBF cells

diff --git a/lib/anim/shp.lua b/lib/anim/shp.lua
index 3092305..c725427 100644
--- a/lib/anim/shp.lua
+++ b/lib/anim/shp.lua
@@ -17,11 +17,12 @@ function SF:read_dbf()
     self.day = bio.read_byte(fp)
     self.nrecs = bio.read_leu32(fp)
     fp:seek("cur", 24)
+    local trim_nul = function (s) return s:gsub("%z", "") end
     local reclen = 0
     local fields = {}
     local byte = bio.read_byte(fp)
     while byte ~= 0x0D do
-        local field_name = util.rtrim(string.char(byte) .. fp:read(10), "\000")
+        local field_name = trim_nul(string.char(byte) .. fp:read(10))
         local field_type = fp:read(1)
         fp:seek("cur", 4)
         local field_length = bio.read_byte(fp)
@@ -45,7 +46,7 @@ function SF:read_dbf()
             local row = {}
             for j = 1, #fields do
                 local field = fields[j]
-                local cell = util.rtrim(fp:read(field.length), " ")
+                local cell = trim_nul(fp:read(field.length))
                 if field.type == "F" or field.type == "N" then
                     cell = tonumber(cell) or 0
                 end

diff --git a/lib/anim/util.lua b/lib/anim/util.lua
index 63f141f..484e4f1 100644
--- a/lib/anim/util.lua
+++ b/lib/anim/util.lua
@@ -12,14 +12,6 @@ local function round(x)
     return i
 end
 
-local function rtrim(s, c)
-    local i = #s
-    while s:sub(i, i) == c do
-        i = i - 1
-    end
-    return s:sub(1, i)
-end
-
 local function startswith(s1, s2)
     return s1:sub(1, #s2) == s2
 end
@@ -40,6 +32,6 @@ end
 
 return {
     hypot=hypot, copysign=copysign, round=round,
-    rtrim=rtrim, startswith=startswith,
+    startswith=startswith,
     func_iter=func_iter
 }