07-06-2007, 09:13 PM
|
#1
|
|
Chief Breaks-many-phones
Join Date: Jun 2007
Location: new york city
Posts: 527
|
Optimize to the best of your ability these two String prototypes:
Code:
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:
Code:
var mystr = "Line 1" + chr(13) + "Line 2" + chr(13) + "Line 3" + chr(13) + "Line 4" + chr(13) + "Line 5";
trace( mystr._getLine(3) );
|
|
|
07-11-2007, 03:07 AM
|
#2
|
|
it's all about patience
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
|
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?
__________________
Ali Inhan
Turkish graphic and web designer
an Apple fan
www.aliinhan.com
|
|
|
07-11-2007, 03:21 AM
|
#3
|
|
it's all about patience
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
|
ActionScript Code:
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?
__________________
Ali Inhan
Turkish graphic and web designer
an Apple fan
www.aliinhan.com
Last edited by inhan; 07-11-2007 at 03:23 AM.
|
|
|
07-11-2007, 04:31 AM
|
#4
|
|
;)
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
|
or this????
ActionScript Code:
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"));
|
|
|
07-11-2007, 09:27 PM
|
#5
|
|
Chief Breaks-many-phones
Join Date: Jun 2007
Location: new york city
Posts: 527
|
Quote:
Originally Posted by inhan
ActionScript Code:
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?
|
|
|
07-11-2007, 10:06 PM
|
#6
|
|
;)
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
|
O_ó
.... what is supposed to be returned on _getLineByString????? false?????
ActionScript Code:
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"));
|
|
|
07-11-2007, 10:11 PM
|
#7
|
|
it's all about patience
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
|
I think we're trying to get the line number of a string with that function. Is that right, Matt?
__________________
Ali Inhan
Turkish graphic and web designer
an Apple fan
www.aliinhan.com
|
|
|
07-11-2007, 10:19 PM
|
#8
|
|
;)
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
|
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.
ActionScript Code:
if (temp[i].indexOf(str) != -1) return Number(i)+1;
|
|
|
07-11-2007, 10:25 PM
|
#9
|
|
Chief Breaks-many-phones
Join Date: Jun 2007
Location: new york city
Posts: 527
|
No, it returns the actual line its on.. use that function and this here code to see what I mean:
Code:
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
|
|
|
07-11-2007, 10:43 PM
|
#10
|
|
;)
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
|
mmmmmmmmmhhhhhhhhhhh.....
:/
so the function i posted return the same!?
ActionScript Code:
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)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 09:06 PM.
///
|
|