This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Konlab's profile picture

[CC 1.63] strplus - String API +

Started by Konlab, 27 June 2014 - 12:01 PM
Konlab #1
Posted 27 June 2014 - 02:01 PM
This is my new API, that can do things with strings.
Note: I can't use patterns, and this API don't use any pattern

Download:
http://pastebin.com/h35zq88B

Function listing:
SpoilertoCharTable("Hello")
returns the char table of string
In our example:
{
"H";
"e";
"l";
"l";
"o"
}

fromCharTable({"H";"i"})
returns string created from CharTable
In our Example: "Hi"

separate("09-05-64-72","-")
separates a string (1. arg) with a separator (2. arg)
returns a table
In our example:
{
"09"
"05"
"64"
"72"
}

unseparate({"09";"05";"64";"72"},"-")
returns the original string
1.arg: table
2.arg: separator
In our example returns: "09-05-64-72"

replace("Hello","l","e")
replaces in string(1.arg) character in 2. arg to character in 3.arg
you can use as 3. arg strings, too.
In our example returns: "Heeeo"

indexChar("HELLO",4)
in our example:
returns 4th character of Hello , so character L

credits()
returns credits

Credits:
SpoilerAll credits to me (Konlab)
Created by Konlab, inspired by tomass1996's StrUtils API, but don't used any part of that API, I (Konlab) don't saw the StrUtils' code before creating this API, you can edit, redistribute, copy etc. if you give me credits

Current version is 1.0
If you find any bugs/ if you have suggestions, post, and I'll fix/add (if I can do that)
Edited on 27 June 2014 - 01:55 PM
Cranium #2
Posted 27 June 2014 - 03:00 PM
Please PM a moderator when you have code to show.
Lyqyd #3
Posted 27 June 2014 - 03:31 PM
Unlocked so code may be posted.
theoriginalbit #4
Posted 27 June 2014 - 05:52 PM
Taking a look into some Lua APIs may be beneficial for you, specifically the String and Table APIs. knowing these will yield the following code changes.

fromCharTableYour code

function fromCharTable(table)
  if type(table) ~= "table" then
	error("Char table needed, got "..type(table))
	return
  end
  local str = ""
  for k,v in pairs(table) do
	str = str..v
  end
  return str
end

Improved code

function fromCharTable(tbl) --# naming table was very bad, you'd not be able to use the table API in this function
  if type(tbl) ~= "table" then
	error("Char table needed, got "..type(tbl))
	return
  end
  return table.concat(tbl)
end

replaceYour code

function replace(str,toreplace,replacewith)
  if type(str) ~= "string" or type(toreplace) ~= "string" or type(replacewith) ~= "string" then
	error("Cannot replace a "..type(str).." if thing to replace is "..type(toreplace).." and the thing with replace is "..type(replacewith))
  end
  t = {}
  for i=1,#str do
	t[i] = str:sub(i,i)
  end
  local retstr = ""
  for i=1,#t do
	if t[i] == toreplace then
	  retstr = retstr .. replacewith
	else
	  retstr = retstr .. t[i]
	end
  end
  return retstr
end

Improved code

function replace(str,toreplace,replacewith)
  if type(str) ~= "string" or type(toreplace) ~= "string" or type(replacewith) ~= "string" then
	error("Cannot replace a "..type(str).." if thing to replace is "..type(toreplace).." and the thing with replace is "..type(replacewith))
  end
  return (str:gsub(toreplace, replacewith))
end

separateusing patterns (yes I know you said you didn't know them) you can change this function like so

Your code

function separate(str,separator)
  if type(str) ~= "string" then
	error("String needed, got "..type(str))
  elseif type(separator) ~= "string" then
	error("Separator:String needed, got "..type(separator))
  end
  local t={}
  for i=1,#str do
	t[i] = str:sub(i,i)
  end
  local t2 = {}
  local cstr = ""
  local cindex = 0
  for i=1,#t do
	if t[i] == separator then
	  cindex = cindex+1
	  t2[cindex] = cstr
	  cstr = ""
	else
	  cstr = cstr..t[i]
	end
  end
  if cstr ~= "" then
	t2[cindex+1] = cstr
  end
  return t2
end

Improved code

function separate(str, separator)
  if type(str) ~= "string" then
	error("String needed, got "..type(str))
  elseif type(separator) ~= "string" then
	error("Separator:String needed, got "..type(separator))
  end
  local t = {}
  for s in str:gmatch("[^"..separator.."]+") do
	t[#t+1] = s
  end
  return t
end

unseparateYour code

function unseparate(table,separator)
  if type(table) ~= "table" then error("Table needed, got "..type(table)) end
  if type(separator) ~= "string" then error("String needed as separator, got "..type(separator)) end
  local string = ""
  for i=1,#table do
	string = string..table[i]..separator
  end
  return string
end

Improved code

function unseparate(tbl, separator) --# again, don't use `table`
  if type(tbl) ~= "table" then error("Table needed, got "..type(tbl)) end
  if type(separator) ~= "string" then error("String needed as separator, got "..type(separator)) end
  return table.concat(tbl, separator)
end

Other notable areas of improvement for learning purposesthe first is with your error calls, the way you've done them, like this

error("message")
will say something like yourApiFileName:lineOfErrorCall: message however it's not your fault they've called your API wrong, so you want to blame them instead of you, so you can use a throwback level on the error function like so

error("message", 2)
which will error saying something like theirProgramFileName:lineTheyCalledYourApiFrom: message meaning they realise that they've done something wrong, and it wasn't you that did something wrong

another area I feel like pointing out is with this code

function indexChar(str,index)
  --# snip arg check
  local char = str:sub(index,index)
  return char
end
you can simply surround the function call in brackets, like this

function indexChar(str,index)
  --# snip arg check
  return (str:sub(index,index))
end
and Lua will only retain the first return value

also with this function
[code]
function credits()
  local credits = "Created by Konlab, inspired by tomass1996's StrUtils API, but don't used any part of that API, I (Konlab) don't saw the StrUtils' code before creating this API, you can edit, redistribute, copy etc. if you give me credits"
  return credits  
end

you could just make

function credits()
  return "Created by Konlab, inspired by tomass1996's StrUtils API, but don't used any part of that API, I (Konlab) don't saw the StrUtils' code before creating this API, you can edit, redistribute, copy etc. if you give me credits"
end
Konlab #5
Posted 27 June 2014 - 08:23 PM
1.I don't knew what is table.concat()
2.I don't knew how to use string.gsub(), because it was in 'pattern' category
3.yes, patterns
4.same as 1.
5.:
-error() things-I don't knew about 2. argument of error()

Oh.
Thanks for this post, it was useful.

Again, thanks!

I'm new at string API, I started to learn it because i needed seprate function.
theoriginalbit #6
Posted 28 June 2014 - 04:45 AM
2.I don't knew how to use string.gsub(), because it was in 'pattern' category
well actually it isn't, they say it takes a pattern on the tutorial websites, but it can also just take a standard string as opposed to a pattern too.
-error() things-I don't knew about 2. argument of error()
yeah you can supply a second argument, it doesn't have to be two either. here is a list of throwbacks
0 = blame no one, just print the message, no filename, no line numbers
1 = this is the same as not providing a number, it will blame you at the line you call error
2 = blame the function that called you
3 = blame the function that called the above one
4 = blame the function that called the above one
5 = blame the function that called the above one
… etc

eventually you'll get stuck in a loop where one level will be the shell, the next bios, the next pcall, and then it will loop back to shell again.

knowing this it can be quite handy to do things like so

local function validate( param, expected )
  if type(param) ~= expected then
    error( "expected "..expected..", got "..type(param), 3)
  end
end


function foo( str )
  validate( str, "string" )
  --# code
end

function bar( str, pat, count )
  validate( str, "string" )
  validate( pat, "string" )
  validate( count, "number" )
  --# code
end
so this validation will check the argument you've told it to, and then error back to your function callers.

Thanks for this post, it was useful.
you're welcome

I'm new at string API, I started to learn it because i needed seprate function.
yeah I made one a while back; StringX
Konlab #7
Posted 28 June 2014 - 05:26 PM
yeah I made one a while back; StringX

Oh , Feb 2013, that's old oh
At Feb 2013 I doesn't played minecraft even!
Edited on 28 June 2014 - 03:26 PM
theoriginalbit #8
Posted 28 June 2014 - 05:28 PM
Oh , Feb 2013, that's old oh
At Feb 2013 I doesn't played minecraft even!
I've been using ComputerCraft since April 2012.
Agoldfish #9
Posted 28 June 2014 - 06:17 PM
Oh , Feb 2013, that's old oh
At Feb 2013 I doesn't played minecraft even!
I've been using ComputerCraft since April 2012.
I've been playing since October 2013, I think.
Konlab #10
Posted 29 June 2014 - 09:02 AM
I joined these forums this year, maybe in april (?)

Also table.concat don't work
theoriginalbit #11
Posted 29 June 2014 - 02:32 PM
Also table.concat don't work
oh yes it does. table.concat