Skif-off left a comment (geany/geany-plugins#1463)

@advice2020,
First of all, I think there is a more elegant solution :)) Secondly, characters 
can have different sizes (1 byte, 2 bytes, 3 bytes, and so on), so...
1)
```lua
-- (cross-platform)
-- 2025.07.01
--[[
https://github.com/geany/geany-plugins/issues/1463
]]

local function StringSplit(sdata, sdelim)
  local ares = {}
  local n1, n2, n3, c = 1, 1, 1, 1
  while true do
    n1, n2 = string.find(sdata, sdelim, n3, true)
    if n1 ~= nil then
      ares[c] = string.sub(sdata, n3, n1 - 1)
      c = c + 1
    else
      ares[c] = string.sub(sdata, n3, -1)
      break
    end
    n3 = n2 + 1
  end
  return ares
end

local function GetTableUTF8Char(s)
  local l = string.len(s)
  local i, j = 1, 1
  local t = {}
  local n
  -- rfc3629
  while true do
    if i > l then break end
    n = string.byte(s, i)
    if (n >= 0) and (n <= 127) then
      t[j] = string.sub(s, i, i)
      i = i + 1
    elseif (n >= 194) and (n <= 223) then
      t[j] = string.sub(s, i, i + 1)
      i = i + 2
    elseif (n >= 224) and (n <= 239) then
      t[j] = string.sub(s, i, i + 2)
      i = i + 3
    elseif (n >= 240) and (n <= 244) then
      t[j] = string.sub(s, i, i + 3)
      i = i + 4
    end
    j = j + 1
  end
  return t
end

local aEOL = {"\r\n", "\r", "\n"}
local iEOL = geany.scintilla("SCI_GETEOLMODE")
local sEOL = aEOL[iEOL + 1]

local bFullText = false
local sText = geany.selection()
if sText == nil then
  geany.message("Fail.")
  return
elseif sText == "" then
  sText = geany.text()
  bFullText = true
end

local aTmp
local aText = StringSplit(sText, sEOL)
for i = 1, #aText do
  aTmp = GetTableUTF8Char(aText[i])
  if #aTmp > 10 then
    aText[i] = table.concat(aTmp, "", 1, 10)
  end
end
local sRes = table.concat(aText, sEOL)
if bFullText == true then
  geany.text(sRes)
else
  geany.selection(sRes)
end
```
2)
```lua
-- (cross-platform)
-- 2025.07.01
--[[
https://github.com/geany/geany-plugins/issues/1463
]]

local function StringSplit(sdata, sdelim)
  local ares = {}
  local n1, n2, n3, c = 1, 1, 1, 1
  while true do
    n1, n2 = string.find(sdata, sdelim, n3, true)
    if n1 ~= nil then
      ares[c] = string.sub(sdata, n3, n1 - 1)
      c = c + 1
    else
      ares[c] = string.sub(sdata, n3, -1)
      break
    end
    n3 = n2 + 1
  end
  return ares
end

local function GetTableUTF8Char(s)
  local l = string.len(s)
  local i, j = 1, 1
  local t = {}
  local n
  -- rfc3629
  while true do
    if i > l then break end
    n = string.byte(s, i)
    if (n >= 0) and (n <= 127) then
      t[j] = string.sub(s, i, i)
      i = i + 1
    elseif (n >= 194) and (n <= 223) then
      t[j] = string.sub(s, i, i + 1)
      i = i + 2
    elseif (n >= 224) and (n <= 239) then
      t[j] = string.sub(s, i, i + 2)
      i = i + 3
    elseif (n >= 240) and (n <= 244) then
      t[j] = string.sub(s, i, i + 3)
      i = i + 4
    end
    j = j + 1
  end
  return t
end

local aEOL = {"\r\n", "\r", "\n"}
local iEOL = geany.scintilla("SCI_GETEOLMODE")
local sEOL = aEOL[iEOL + 1]

local bFullText = false
local sText = geany.selection()
if sText == nil then
  geany.message("Fail.")
  return
elseif sText == "" then
  sText = geany.text()
  bFullText = true
end

local aTmp, sTmp, iC, bR
local aRes = {}
local aText = StringSplit(sText, sEOL)
for i = 1, #aText do
  aTmp = GetTableUTF8Char(aText[i])
  if #aTmp > 10 then
    iC = 0
    while true do
      bR, sTmp = pcall(table.concat, aTmp, "", iC + 1, iC + 10)
      if bR == false then
        if aTmp[iC + 1] ~= nil then
          sTmp = table.concat(aTmp, "", iC + 1, #aTmp)
          table.insert(aRes, sTmp)
        else
          break
        end
      else
        table.insert(aRes, sTmp)
      end
      iC = iC + 10
    end
  else
    table.insert(aRes, aText[i])
  end
end
local sRes = table.concat(aRes, sEOL)
if bFullText == true then
  geany.text(sRes)
else
  geany.selection(sRes)
end
```
P.S. Thirdly, I found out that `pcall` is now my favorite function in Lua.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany-plugins/issues/1463#issuecomment-3024797200
You are receiving this because you are subscribed to this thread.

Message ID: <geany/geany-plugins/issues/1463/[email protected]>

Reply via email to