Difference between revisions of "String (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(CyouBemimMG)
(Undo revision 4703 by 46.231.129.69 (talk))
 
Line 1: Line 1:
ssahraj1          @sambottaWatch me as I dance under the spotlight- Listen to the peolpe screaming out more and more,  Coz I create the feeling that keep em coming back, Yeah, I create the feeling that keep em coming back, So captivating when I get it on the floor.   Know y'all been patiently waiting, I know you need me, I can feel it,  I'm a beast, I'm an animal, I'm that monster in the mirror, The headliner, finisher, I'm the closer, winner. Best when under pressure with second's left I show up. .
+
The string API is a default Lua 5.1 API as defined [http://www.lua.org/manual/5.1/manual.html#5.4 here]. Please list any non-working functions below.
 +
 
 +
==Non-Working Functions==
 +
 
 +
===string.gmatch(string, pattern)===
 +
 
 +
Using any quantifier other than + will cause it to hang if that quantifier applies to the whole pattern and that pattern does not match the whole string.
 +
 
 +
Examples:
 +
<pre>
 +
"abcdef":gmatch("%a*")  -- Works
 +
"abc=def":gmatch("%a*")  -- Hang
 +
"abc=def":gmatch("%a.*") -- Works
 +
"abc=def":gmatch("[%a.]*") -- Hang
 +
"abc=def":gmatch("[^=]*")  -- Hang
 +
"abc=def":gmatch("([^=]*)=?") -- Works (Note: This produces the same result as the above pattern should)
 +
"abcdef":gmatch("[^=]*") -- Works
 +
"abc=def":gmatch("[^=]*=") -- Works
 +
</pre>
 +
 
 +
===string.sub / string.find ===
 +
 
 +
Both string.sub and string.find work on their own, but calling string.find on a string returned by string.sub will return the index of the character in the original string, not the sub string you are referencing.
 +
 
 +
Example:
 +
<pre>
 +
local testString = "This is a sample string";
 +
local testSubString = string.sub(testString,5);
 +
 
 +
local indexOfSample = string.find(testSubString,"sample");
 +
</pre>
 +
 
 +
indexOfSample is returned 11 instead of the expected 7
 +
 
 +
Concatenating a blank string onto the end of the string causes new memory to be allocated and works properly again
 +
 
 +
<pre>
 +
local testString = "This is a sample string";
 +
local testSubString = string.sub(testString,5);
 +
 
 +
local indexOfSample = string.find(testSubString.."","sample");
 +
</pre>
 +
 
 +
This is also a problem with using string.find on command line arguments, as this returns the index in the full command path instead of the specific argument.
 +
 
 +
 
 +
[[Category:APIs]]

Latest revision as of 03:24, 7 December 2012

The string API is a default Lua 5.1 API as defined here. Please list any non-working functions below.

Non-Working Functions

string.gmatch(string, pattern)

Using any quantifier other than + will cause it to hang if that quantifier applies to the whole pattern and that pattern does not match the whole string.

Examples:

"abcdef":gmatch("%a*")   -- Works
"abc=def":gmatch("%a*")   -- Hang
"abc=def":gmatch("%a.*")  -- Works
"abc=def":gmatch("[%a.]*")  -- Hang
"abc=def":gmatch("[^=]*")  -- Hang
"abc=def":gmatch("([^=]*)=?") -- Works (Note: This produces the same result as the above pattern should)
"abcdef":gmatch("[^=]*")  -- Works
"abc=def":gmatch("[^=]*=")  -- Works

string.sub / string.find

Both string.sub and string.find work on their own, but calling string.find on a string returned by string.sub will return the index of the character in the original string, not the sub string you are referencing.

Example:

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);

local indexOfSample = string.find(testSubString,"sample");

indexOfSample is returned 11 instead of the expected 7

Concatenating a blank string onto the end of the string causes new memory to be allocated and works properly again

local testString = "This is a sample string";
local testSubString = string.sub(testString,5);

local indexOfSample = string.find(testSubString.."","sample");

This is also a problem with using string.find on command line arguments, as this returns the index in the full command path instead of the specific argument.