I suppose the most used, generaly speacking, is trim.
As you said, trim is the result of left and right trim but since what people
need is trim speed, I do not think is a good idea to create a trim function
that return trimstart(trimend(text)) so the code has to be a little bit
redundant, but trim performances will be preserved (avoiding two function
calls for each trim)

As sum, what do you think about this simple plugin?

;jQuery.extend({
    trimstart:function( text ){
        if( text ){
            var start = -1;
            while( text.charCodeAt( ++start ) < 33 );
            return text.substring( start );
        };
        return "";
    },
    trimend:function( text ){
        if( text ){
            var end = text.length;
            while( text.charCodeAt( --end ) < 33 );
            return text.substring( 0, end + 1 );
        };
        return "";
    }
});


On Wed, Nov 5, 2008 at 9:33 AM, Anthony Johnston <
[EMAIL PROTECTED]> wrote:

>  Could you split this so that we have trimstart and trimend?
>
>
>
> Ant
>
>
>
> *From:* [email protected] [mailto:[EMAIL PROTECTED] *On
> Behalf Of *Andrea Giammarchi
> *Sent:* 05 November 2008 09:03
> *To:* [email protected]
> *Subject:* [jquery-dev] Re: Fast trim implementation
>
>
>
> I did just a couple of tests, and this seems to be the most compact and
> fast in any case.
>
> function myBestTrim( str ){
>     var start = -1, end = str.length;
>     while(str.charCodeAt( --end ) < 33);
>     while(str.charCodeAt( ++start ) < 33);
>     return str.slice( start, end + 1 );
> };
>
> There is probably one case where it does not perform faster, length 1 or 0,
> but we are talking abut 1 to 5 miliseconds in both cases, and about "0" with
> new browsers possibilities.
>
> 2 proposals:
>
> trim: function( text ) {
>         var str = text || "", start = -1, end = str.length;
>         while(str.charCodeAt( --end ) < 33);
>         while(str.charCodeAt( ++start ) < 33);
>         return str.slice( start, end + 1 );
>     },
>
>     trim: function( text ) {
>         if(text){
>             var start = -1, end = text.length;
>             while( text.charCodeAt( --end ) < 33 );
>             while( text.charCodeAt( ++start ) < 33 );
>             return text.slice( start, end + 1 );
>         };
>         return "";
>     },
>
> Regards
>
> On Tue, Nov 4, 2008 at 10:14 PM, Andrea Giammarchi <
> [EMAIL PROTECTED]> wrote:
>
> Cheers :-)
>
>
>
> On Tue, Nov 4, 2008 at 10:11 PM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
>
>
> Will benchmark all this.
>
> On Tue, Nov 4, 2008 at 7:10 PM, Andrea Giammarchi
>
> <[EMAIL PROTECTED]> wrote:
>
> > But in this case you are checking twice for each character, start less
> than
> > end plus the other check.
> > For small strings the best option culd be return the regexp version
> > /^\s+|\s+$/g while for strings with length more than N (dunno which
> length
> > is big enough) we can use my last proposal without problems, a charcode
> > instead of start < end wont make the difference, IMO :-)
> >
> > On Tue, Nov 4, 2008 at 10:02 PM, Ariel Flesler <[EMAIL PROTECTED]>
> wrote:
> >>
> >> That does, but will iterate doubly for strings made of spaces. That's
> >> why I added the check.
> >
> >
> > >
> >
>
>
>    --
> Ariel Flesler
> http://flesler.blogspot.com
>
>
>
>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to