Jump to content




[CC1.3]String Utils Api



  • You cannot reply to this topic
48 replies to this topic

#21 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 03 March 2012 - 08:36 AM

Added: contains(string,find) --Returns true if @string contains @find

#22 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 04 March 2012 - 10:10 AM

Another update:
Re-factoring of code,
Added:
CRC32(str) --Returns CRC32 Hash of @str
FCS16(str) --Returns FCS16 Hash of @str
FCS32(str) --Returns FCS32 Hash of @str
replace(str, from, to) --Replaces @from to @to in @str

#23 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 04 March 2012 - 12:24 PM

just to make it clear: does your API contain the necessary license note? If so, is it OK if I ship it with my OS?

#24 Garthog

  • New Members
  • 3 posts

Posted 05 March 2012 - 12:39 AM

I've got an error when trying to decrypt unencrypted string. I'm making a crypted communication between turtles and a computer, but if someone broadcast a simple string like test, it will crash all my turtles with error : StrUtil:614 bad argument #1: invalid value

How to reproduce :

StrUtil.decrypt("test", "somekey")


#25 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 05 March 2012 - 02:51 AM

View PostLiraal, on 04 March 2012 - 12:24 PM, said:

just to make it clear: does your API contain the necessary license note? If so, is it OK if I ship it with my OS?

Yes the license is included, There is a separate license file included in the download, But it is also included at the top of the API file. Yes it is fine, as long as you clearly state on the thread that it is mine.

View PostGarthog, on 05 March 2012 - 12:39 AM, said:

I've got an error when trying to decrypt unencrypted string. I'm making a crypted communication between turtles and a computer, but if someone broadcast a simple string like test, it will crash all my turtles with error : StrUtil:614 bad argument #1: invalid value

How to reproduce :

StrUtil.decrypt("test", "somekey")

It is because you are trying to decrypt an unencrypted string.

#26 Garthog

  • New Members
  • 3 posts

Posted 05 March 2012 - 10:57 AM

Sure, but it shouldn't crash the whole program.

#27 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 05 March 2012 - 11:23 AM

View PostGarthog, on 05 March 2012 - 10:57 AM, said:

Sure, but it shouldn't crash the whole program.

Quick Fix:
Open String Utils change the decrypt function to:
function decrypt(str, key)  --Decrypts @str with @key
if not key then return nil end
str = tostring(str)
local key = SHA1(key)
local strLen = str:len()
local keyLen = key:len()
local j=1
local result = ""
for i=1, strLen, 2 do
  local ordStr = basen(tonumber(string.reverse(str:sub(i, i+1)),36),10)
  if j==keyLen then j=1 end
  local ordKey = string.byte(key:sub(j,j))
  local nxChar = ordStr-ordKey
  if nxChar<0 or nxChar>255 then nxChar = 0 end
  result = result..string.char(nxChar)
  j = j+1
end
return result:sub(11)
end


#28 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 29 March 2012 - 10:39 AM

function isEmailAddress(str) --Checks if @str is a valid email address
	if not str then return nil end
	str = tostring(str)
    if (str:match("[A-Za-z0-9%.%%%+%-][email protected][A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")) then
	    return true
    else
	    return false
    end
end

Does this match all valid email addresses?
They don't necessarily contain dots or 2-4 letter TLDs. Or even alphabetic TLDs.

[email protected] is a perfectly valid email address, so is [email protected] (even though .music doesn't exist) or [email protected] or even "hi there!"@foo (if I interpreted RFC 822 correctly)

#29 DerrikeG

  • New Members
  • 11 posts

Posted 25 May 2012 - 04:00 AM

function trim(str)  --Trim @str of initial/trailing whitespace
	if not str then return nil end
	str = tostring(str)
	return (str:gsub("^%s*(.-)%s*$", "%1"))
end
The above code could be written as:
function trim(str)  --Trim @str of initial/trailing whitespace
	return tostring(str):gsub("^%s*(.-)%s*$", "%1")
end
Both functions here would successfully return nil if passed nil, enabling you to return nil.Also assigning the variable as its tostringed self is unnecessary since you can pass the return of tostring to gsub as demonstrated without needing it set to a variable.
Wrapping the return in parenthesis only suppresses the gsub's additional return values but there's no reason to do that since it helps someone to determine whether or not the string was actually trimmed.

#30 Graypup

  • Members
  • 90 posts

Posted 01 July 2012 - 07:22 PM

Can I bundle your SHA1 in my OS so that I don't have a dependency problem?

#31 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 02 July 2012 - 12:37 AM

View PostGraypup, on 01 July 2012 - 07:22 PM, said:

Can I bundle your SHA1 in my OS so that I don't have a dependency problem?

Here's the file you'll need with only the SHA1 stuff in it for you: https://bitbucket.or...5fc0ec/SHA1.lua

#32 Graypup

  • Members
  • 90 posts

Posted 02 July 2012 - 04:14 PM

I copied the contents of the file into my API. Thanks!
I'm trying to make the OS as secure as possible, so encryption certainly helped.

#33 nintendofan345

  • New Members
  • 2 posts

Posted 06 July 2012 - 12:35 AM

Does this work with the read() in the common password doors? I'm trying to use toHex like this:

StrUtil.toHex(read())

But when I run the program it fails with the following error:

startup:4: attempt to index ? (a nil value)

I really don't know how to fix this except by using read() by itself. Thanks in advance for any help.

#34 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 06 July 2012 - 12:44 AM

View Postnintendofan345, on 06 July 2012 - 12:35 AM, said:

Does this work with the read() in the common password doors? I'm trying to use toHex like this:

StrUtil.toHex(read())

But when I run the program it fails with the following error:

startup:4: attempt to index ? (a nil value)

I really don't know how to fix this except by using read() by itself. Thanks in advance for any help.
Looks like the api is not loaded, it might be an installation error, maybe the file has another name.
EDIT:
Try with StrUtils instead of StrUtil, that should fix it

#35 nintendofan345

  • New Members
  • 2 posts

Posted 06 July 2012 - 01:26 AM

Oh, that worked, thank you.

#36 Graypup

  • Members
  • 90 posts

Posted 28 July 2012 - 10:46 PM

*fixed*

#37 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 05 August 2012 - 10:31 PM

when i installed this i opened the lua prompt and put toOctal ("hi") and i got attempt to call nil

#38 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 05 August 2012 - 10:36 PM

View PostETHANATOR360, on 05 August 2012 - 10:31 PM, said:

when i installed this i opened the lua prompt and put toOctal ("hi") and i got attempt to call nil

You need to put the name the api is saved as in front.
So if you saved it as StrUtil then you would do
StrUtil.toOctal("Hi")

#39 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 05 August 2012 - 11:39 PM

thanks :P/>

#40 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 05 August 2012 - 11:49 PM

i saved it as SU and typed SU.toOctal("hi") and got attempt to index a nil value. what am i doing wrong now?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users