Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Community Boards > Just for Kicks Challenges

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-06-2007, 09:13 PM   #1
mattkenefick
Chief Breaks-many-phones
 
Join Date: Jun 2007
Location: new york city
Posts: 527
Default 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) );
mattkenefick is offline   Reply With Quote
Old 07-11-2007, 03:07 AM   #2
inhan
it's all about patience
 
inhan's Avatar
 
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
Default

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
inhan is offline   Reply With Quote
Old 07-11-2007, 03:21 AM   #3
inhan
it's all about patience
 
inhan's Avatar
 
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
Default

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.
inhan is offline   Reply With Quote
Old 07-11-2007, 04:31 AM   #4
3pepe3
;)
 
3pepe3's Avatar
 
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
Default

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"));
__________________
http://www.pepemagana.com
3pepe3 is offline   Reply With Quote
Old 07-11-2007, 09:27 PM   #5
mattkenefick
Chief Breaks-many-phones
 
Join Date: Jun 2007
Location: new york city
Posts: 527
Default

Quote:
Originally Posted by inhan View Post
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?
mattkenefick is offline   Reply With Quote
Old 07-11-2007, 10:06 PM   #6
3pepe3
;)
 
3pepe3's Avatar
 
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
Default

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"));
__________________
http://www.pepemagana.com
3pepe3 is offline   Reply With Quote
Old 07-11-2007, 10:11 PM   #7
inhan
it's all about patience
 
inhan's Avatar
 
Join Date: Jun 2005
Location: istanbul
Posts: 6,696
Default

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
inhan is offline   Reply With Quote
Old 07-11-2007, 10:19 PM   #8
3pepe3
;)
 
3pepe3's Avatar
 
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
Default

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;
__________________
http://www.pepemagana.com
3pepe3 is offline   Reply With Quote
Old 07-11-2007, 10:25 PM   #9
mattkenefick
Chief Breaks-many-phones
 
Join Date: Jun 2007
Location: new york city
Posts: 527
Default

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
mattkenefick is offline   Reply With Quote
Old 07-11-2007, 10:43 PM   #10
3pepe3
;)
 
3pepe3's Avatar
 
Join Date: Aug 2006
Location: In transit--- Still bored
Posts: 1,739
Default

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)
__________________
http://www.pepemagana.com
3pepe3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Useful Utilities..cookies, key combination,etc MichaelxxOA Projectors and CDs 147 11-08-2013 11:28 AM
Parsing mtanti ActionScript 2.0 4 03-11-2010 11:43 PM
new string functions! tell me what you need! colfaxrev ActionScript 2.0 11 09-23-2008 09:59 PM
Accessing a string from a variable input text field matbury ActionScript 2.0 10 05-19-2007 11:09 PM
06++ = 7 != 07 Flash Gordon Server-Side Scripting 5 01-17-2006 06:27 PM


All times are GMT. The time now is 01:09 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.