PDA

View Full Version : Optimize to the best of your ability these two String prototypes:


mattkenefick
07-06-2007, 10:13 PM
String.prototype._charCount = function(str) {
var cnt = 0, getPos = 0;

while(getPos<this.lastIndexOf(str))
getPos = this.indexOf(str,getPos)+1, cnt++

return cnt;
}

String.prototype._getLine = function(num){
var getPos =0;
var tmpStr;

// if theres no break at the end, add one
if(this.charAt(this.length) != chr(13)) this=this+chr(13);

// run through all line breaks in string
for(var i = 0; i<num-1; i++)
getPos = this.indexOf(chr(13),getPos)+1;

// get the string
tmpStr = this.substr( getPos, this.indexOf(chr(13), getPos+1)-getPos )
return tmpStr
}

String.prototype._getLineByString = function(str){
var getPos =0;
var tmpStr;
var lb = chr(13);
var lines = this._charCount( lb );

// if theres no break at the end, add one
if(this.charAt(this.length) != lb) this=this+lb;

// run through all line breaks in string
for(var i = 0; i<this._charCount( lb ); i++){
tmpStr = this.substr( getPos, this.indexOf(lb, getPos+1)-getPos )
getPos = this.indexOf(lb,getPos)+1;
if(tmpStr.indexOf(str)>-1) return tmpStr;
}

// get the end
return false
}




Usage:



var mystr = "Line 1" + chr(13) + "Line 2" + chr(13) + "Line 3" + chr(13) + "Line 4" + chr(13) + "Line 5";
trace( mystr._getLine(3) );

inhan
07-11-2007, 04:07 AM
There are 3 prototypes in there. And what do you mean with optimizing? Making it shorter? Making it/them more processor friendly? Or recreate prototypes that are doing the exact same thing?

inhan
07-11-2007, 04:21 AM
String.prototype.$charCount = function(str:String):Number {
return this.split(str).length - 1;
}

String.prototype.$getLine = function(num:Number):String {
return this.split(chr(13))[num - 1];
}

//

Is that what you mean?

3pepe3
07-11-2007, 05:31 AM
or this????

String.prototype._getLine = function(num,breaker) {
breaker=(!breaker)?String.fromCharCode(13):breaker
var temp = this.split(breaker);
return temp[num-1];
};
///Strings
var mystr1 = "Line 1" + String.fromCharCode(13) + "Line 2" + String.fromCharCode(13) + "Line 3" + String.fromCharCode(13) + "Line 4" + String.fromCharCode(13) + "Line 5";
var mystr2 = "another line 1<br>another line 2<br>another line 3<br>another line 4<br>another line 5";
var mystr3 ="or line 1\ror line 2\ror line 3\ror line 4\ror line 5"
///Examples
trace(mystr1._getLine(3));
trace(mystr2._getLine(1,"<br>"));
trace(mystr3._getLine(2,"\r"));

mattkenefick
07-11-2007, 10:27 PM
String.prototype.$charCount = function(str:String):Number {
return this.split(str).length - 1;
}

String.prototype.$getLine = function(num:Number):String {
return this.split(chr(13))[num - 1];
}

//

Is that what you mean?

Yeah.. those are great. Solid one liners. Did you run any speed tests on them yet?

Come up with anything for getLineByString?

3pepe3
07-11-2007, 11:06 PM
O_ó
.... what is supposed to be returned on _getLineByString????? false?????


String.prototype._getLineByString = function(str, breaker) {
breaker = (!breaker) ? String.fromCharCode(13) : breaker;
var temp = this.split(breaker);
for (i in temp) {
if (temp[i].indexOf(str) != -1) return ((temp[i]));
}
};
var mystr = "Line 1"+String.fromCharCode(13)+"Line where pepe is"+String.fromCharCode(13)+"Line 3"+String.fromCharCode(13)+"Line 4"+String.fromCharCode(13)+"Line 5";
trace(mystr._getLineByString("pepe"));

inhan
07-11-2007, 11:11 PM
I think we're trying to get the line number of a string with that function. Is that right, Matt?

3pepe3
07-11-2007, 11:19 PM
testing the original function i just get returned false (becase line bellow "// get the end " is returning)
if _getLineByBtring is trying to return the number where the string is, then
in the previews function cahnging the returned value will give the line where the string is.

if (temp[i].indexOf(str) != -1) return Number(i)+1;

mattkenefick
07-11-2007, 11:25 PM
No, it returns the actual line its on.. use that function and this here code to see what I mean:


var str = "This is a" + chr(13) + "new line break" + chr(13) + "quick brown fox" + chr(13) + "lazy dogg";

trace(str._getLineByString("break"));
// returns new line break

3pepe3
07-11-2007, 11:43 PM
mmmmmmmmmhhhhhhhhhhh.....
:/
so the function i posted return the same!?

String.prototype._getLineByString = function(str, breaker) {
breaker = (!breaker) ? String.fromCharCode(13) : breaker;
var temp = this.split(breaker);
for (i in temp) {
if (temp[i].indexOf(str) != -1) return ((temp[i]));
}
};

///
var str = "This is a"+String.fromCharCode(13)+"new line break"+String.fromCharCode(13)+"quick brown fox"+String.fromCharCode(13)+"lazy dogg";
trace(str._getLineByString("break"));

// returns new line break
///Also see that I use String.fromCharCode(13) instead chr(13)

mattkenefick
07-11-2007, 11:49 PM
is there a reason you use String.fromCharCode( x )?

whats the difference?

3pepe3
07-12-2007, 02:40 AM
it's the same but:
Deprecated since Flash Player 5. This function was deprecated in favor of String.fromCharCode().

but still if you prefer to use chr(13), the optimized function (http://www.actionscript.org/forums/showpost.php3?p=630383&postcount=10) will handle that just adding an argument where you use the function:


var str = "This is a" + chr(13) + "new line break" + chr(13) + "quick brown fox" + chr(13) + "lazy dogg";
trace(str._getLineByString("break",chr(13)));
// |
// |
// |
// v
// The argument

mattkenefick
07-12-2007, 06:55 AM
Close pepe, it didnt work for me though.
Might want to try this:



String.prototype._getLineByString = function(str, breaker) {
// credit to 3pepe3 for rewriting this function : http://www.pepemagana.com
breaker = (!breaker) ? String.fromCharCode(13) : breaker;
var temp = this.split(breaker);
for (var i =0;i<temp.length;++i) {
if (temp[i].indexOf(str) != -1) return (temp[i]);
}
};

inhan
07-12-2007, 08:06 AM
I would use

breaker = breaker || String.fromCharCode(13);

rather than

breaker = (!breaker) ? String.fromCharCode(13) : breaker;

but other than that I can't find anything more efficient than the one you've last posted, Matt. (unless we want it to return false when there's no mathing line :)

String.prototype._getLineByString = function(str, breaker) {
breaker = breaker || String.fromCharCode(13);
var temp = this.split(breaker);
for (var i = 0; i < temp.length; ++i) {
if (temp[i].indexOf(str) != -1) temp = temp[i]; break;
}
return typeof(temp) == "string" ? temp : false;
};

3pepe3
07-12-2007, 09:47 PM
String.prototype._getLineByString = function(str, breaker) {
//Great line inhan i never thought in this kind of construction
breaker = breaker || String.fromCharCode(13);
var temp = this.split(breaker);
///mattkenefick why you use "(var i = 0; i < temp.length; ++i)" instead of:
for (var i in temp) {
if (temp[i].indexOf(str) != -1) return temp[i]
}
return '"'+str+'"'+" not found"
};

mattkenefick
07-12-2007, 10:12 PM
///mattkenefick why you use "(var i = 0; i < temp.length; ++i)" instead of:





When I used yours directly, it didn't work until I switched it out.

3pepe3
07-12-2007, 10:26 PM
weird... if you delete the 'if' line and you put a trace(temp[i]) you must get all the values inside the array.
for (var i in temp) {
trace(temp[i])
//if (temp[i].indexOf(str) != -1) return (temp[i])
}

lazy dogg
quick brown fox
new line break
This is a

mattkenefick
07-12-2007, 11:21 PM
that was the thing.. i didn't, i got a list of the properties.

which is weird I know, but thats what I got so I changed it haha.

3pepe3
07-13-2007, 12:48 AM
okydoky :D
By the way.... can you explain and put the codes on this thread (http://www.actionscript.org/forums/showthread.php3?t=141543)?!?!?
for me it was imposible... and i would love to learn how to accomplish that.

rotabush
01-30-2009, 04:06 AM
Split() works great for (relatively) small strings, but gets very memory intensive for massive values. I am trying to parse large text files and can't see a way to read it in 1 line at a time like Java's StreamReader.readLine() method. Any thoughts?

Thanks,

-rotabush