You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by adithyank <ad...@gmail.com> on 2018/10/12 19:22:22 UTC

More Default String Methods

As suggested by Jochen Theodorou in this 
link
<http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html>  
post, I have created this topic for `More Default String Methods` 

I would like to work in these if the community accepts this proposal

//Method to return left n characters
*1. static String left(String self, int chars)*

//Method to return right n characters
*2. static String right(String self, int chars)*

//Method to return the String that is after the first occurrence of the
given searchString
*3. static String after(String self, String searchString)*

//Method to return the String that is before the first occurrence of the
given searchString
*4. static String before(String self, String searchString)*

//Method to return the String that is before the first '=' character
*5. static String getBeforeEq(String self)*


/**
Splits the given String with the given separator String and returns the
value at the given column Index. By
default, successive occurrence of the separator String is taken are one unit
and the split is performed
*/
*6. static String column(String self, String separator, int colIndex)*


/**
Splits the given String with Space characters and returns the value at the
given column Index.
*/
*7. static String column(String self, int colIndex)*

//Method to return the String that is between the pair of double quotes
*8. static String bwDoubleQuotes(String line)*

//Method to return the String that is between the pair of single quotes
*9. static String bwSingleQuotes(String line)*

//Method to return the String that is between the pair of given enclosure
String
*10. static String bw(String line, String enclosure)*

//Method to return the String that is between the 2 given strings
*11. static String bw(String line, String from, String to)*


//Method to return the String that is between the nth pair of double quotes
*12. static String bwDoubleQuotes(String line, int index)*


//Method to return the String that is between the nth pair of single quotes
*13. static String bwSingleQuotes(String line, int index)*


//Method to return the String that is between the nth pair of given
enclosure String
*14. static String bw(String line, String enclosure, int index)*

//Method to check whether searchString exists within self String ignoring
the case
*15. static boolean containsIgnoreCase(String self, String searchString)*

//Method to check whether the String starts with the searchString ignoring
the case
*16. static boolean startsWithIgnoreCase(String self, String searchString)*

//Method to check whether the String ends with the searchString ignoring the
case
*17. static boolean endsWithIgnoreCase(String self, String searchString)*






--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: More Default String Methods

Posted by "K Adithyan (tech)" <ad...@gmail.com>.
Hi Paul King,

Thank you very much for your time in doing the review.

I have implemented the suggested changes and pushed to the branch in my
fork.
All tests passed. Same PR Link below.

https://github.com/adithyank/groovy/pull/1

Kindly suggest the way forward...!

Thanks,

Adithyan K
India


On Fri, Dec 14, 2018 at 12:58 PM Paul King <pa...@asert.com.au> wrote:

> My suggestions:
>
> * alter your code style to match the existing style
> * rename `after` to `takeAfter`
> * rename `before` to `takeBefore`
> * rename `between` to `takeBetween`
> * no need for `stripLeft` since that is what `drop` does - with a minor
> difference in behavior for the -1 case
> * rename `stripRight` to `dropRight` and align -1 behavior to `drop`
> * no need for GString variants of startsWith/endsWith/containsIgnoreCase
> (CharSequence variant works fine by itself for GString unless we want to
> have a better return type)
> * I'd suggest picking a better name for the occurrence counter, e.g. `int
> occurrence` instead of `int item`
>
> Cheers, Paul.
>
>
> On Sun, Nov 25, 2018 at 9:11 PM adithyank <ad...@gmail.com> wrote:
>
>> I have created a PR for this change and kept in my fork itself. Link below
>>
>> https://github.com/adithyank/groovy/pull/1
>>
>> I have a doubt whether a jira issue be created for every commit...
>>
>> How should I Proceed...
>>
>> Kindly advice
>>
>>
>>
>> -----
>> Thanks,
>>
>> Adithyan K
>> India
>> --
>> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>>
>

Re: More Default String Methods

Posted by Paul King <pa...@asert.com.au>.
My suggestions:

* alter your code style to match the existing style
* rename `after` to `takeAfter`
* rename `before` to `takeBefore`
* rename `between` to `takeBetween`
* no need for `stripLeft` since that is what `drop` does - with a minor
difference in behavior for the -1 case
* rename `stripRight` to `dropRight` and align -1 behavior to `drop`
* no need for GString variants of startsWith/endsWith/containsIgnoreCase
(CharSequence variant works fine by itself for GString unless we want to
have a better return type)
* I'd suggest picking a better name for the occurrence counter, e.g. `int
occurrence` instead of `int item`

Cheers, Paul.


On Sun, Nov 25, 2018 at 9:11 PM adithyank <ad...@gmail.com> wrote:

> I have created a PR for this change and kept in my fork itself. Link below
>
> https://github.com/adithyank/groovy/pull/1
>
> I have a doubt whether a jira issue be created for every commit...
>
> How should I Proceed...
>
> Kindly advice
>
>
>
> -----
> Thanks,
>
> Adithyan K
> India
> --
> Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html
>

Re: More Default String Methods

Posted by adithyank <ad...@gmail.com>.
I have created a PR for this change and kept in my fork itself. Link below

https://github.com/adithyank/groovy/pull/1

I have a doubt whether a jira issue be created for every commit...

How should I Proceed...

Kindly advice



-----
Thanks,

Adithyan K
India
--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: More Default String Methods

Posted by adithyank <ad...@gmail.com>.
There are couple of queries and I responded inline. How should I proceed now?

Can I start coding ? or I have to write the documentation along with
implementation details some where and send the link?

Kindly advice



--
Sent from: http://groovy.329449.n5.nabble.com/Groovy-Dev-f372993.html

Re: More Default String Methods

Posted by "K Adithyan (tech)" <ad...@gmail.com>.
On Mon, Oct 15, 2018 at 1:41 AM MG <mg...@arscreat.com> wrote:

>
>
> On 14.10.2018 11:04, Jochen Theodorou wrote:
> > On 12.10.2018 21:22, adithyank wrote:
> >> As suggested by Jochen Theodorou in this
> >> link
> >> <
> http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html>
>
> >>
> >> post, I have created this topic for `More Default String Methods`
> >>
> >> I would like to work in these if the community accepts this proposal
> >>
> >> //Method to return left n characters
> >> *1. static String left(String self, int chars)*
> >
> > self[0..chars]?
>
> Having a method here might be useful, but I would call it "fromLeft .
> "left" methd to me would be "all the chars left of the given char
> position", which would also be good to have, imho.
>
> ok

> >
> >> //Method to return right n characters
> >> *2. static String right(String self, int chars)*
> >
> > self[-chars..-1]?
>
> Same as "left" above (with the addition that "-chars-1" is easier to get
> wrong ;-) ).
>
> ok

>
> >
> >> //Method to return the String that is after the first occurrence of the
> >> given searchString
> >> *3. static String after(String self, String searchString)*
> >>
> >> //Method to return the String that is before the first occurrence of the
> >> given searchString
> >> *4. static String before(String self, String searchString)*
> >
> > these sound more interesting... empty String if the search string is
> > not in self?
>
> Asking mysself, how often does anyone need that, and if one does need
> it, would he rememeber/find it with that name ?
> Along the same line: Use a regex for special cases like that ?
>

I have build a (in my oragnization) dsl framework over groovy, which is
useful in analysing the log files generated by our server application.
Methods like this are very very helpful for non-developers (support staff,
QA staff, etc) for doing the log analysis, summarising data, etc.

*Ex 1: cat log.txt | gdsl --in 'line.after("Time taken = ").before("
ms").numval / 1000' | sort | uniq -c*
*Ex 2: cat log.txt | gdsl --in 'line.bw <http://line.bw>("Time taken = ", "
ms").numval / 1000' | sort | uniq -c*

Just ignore the '*--in*' option as it is part of my gdsl platform which I
have developed and see the text between the pair of single quotes. That is
valid groovy executable line.

I accept that the same can be done using regex or simple String.indexOf()
and String.substring(). But, friendly methods like this will enable
friendly text processing in the command line itself

Note : 'numval' is a friendly replacement of Groovy's 'toInteger()'...


> >
> >> //Method to return the String that is before the first '=' character
> >> *5. static String getBeforeEq(String self)*
> >
> > which is foo.before("=") if number 4 is taken... This method looks to
> > me a bit overspecialized
>
> I think it is overspecialized in any case. Better to use a class that
> parses e.g. an properties file for you, or go for a full blown parser here.
>
> Yes. Overspecialized. But, acts as friendly call in the command line log
file processing... Just my opinion. I m ok to drop this !


> >
> >> /**
> >> Splits the given String with the given separator String and returns the
> >> value at the given column Index. By
> >> default, successive occurrence of the separator String is taken are
> >> one unit
> >> and the split is performed
> >> */
> >> *6. static String column(String self, String separator, int colIndex)*
> >>
> >> /**
> >> Splits the given String with Space characters and returns the value
> >> at the
> >> given column Index.
> >> */
> >> *7. static String column(String self, int colIndex)*
> >
> > self.split(speparator)[colIndex]?
>
> Also quite a special method, but depending on implementation,
> performance of "column" method woud be much better.
>
> Yes

> >
> >> //Method to return the String that is between the pair of double quotes
> >> *8. static String bwDoubleQuotes(String line)*
> >>
> >> //Method to return the String that is between the pair of single quotes
> >> *9. static String bwSingleQuotes(String line)*
> >
> > again these look a bit overspecialized to me
>
> unquote(String s, String quoteString = '"') ?
>
> >
> >> //Method to return the String that is between the pair of given
> >> enclosure
> >> String
> >> *10. static String bw(String line, String enclosure)*
> >>
> >> //Method to return the String that is between the 2 given strings
> >> *11. static String bw(String line, String from, String to)*
>
> Is this for parsing, or is it more:
> strip(String s, String bracketString) ?
> trim(String s, String trimChars) ?
>
> "bw" is way too short / non-obvious a method name in my book.
>

It is useful for doing command line log file processing. Pls see *Ex 2 *in  my
response to 4th item in this mail.

>
> >>
> >>
> >> //Method to return the String that is between the nth pair of double
> >> quotes
> >> *12. static String bwDoubleQuotes(String line, int index)*
> >>
> >>
> >> //Method to return the String that is between the nth pair of single
> >> quotes
> >> *13. static String bwSingleQuotes(String line, int index)*
> >
> > so 10 is 11 with from=to, 12 is 10 if enclosure " and 13 is 10 with
> > enclosure '. so don`t know about 12 and 13
> >
>
True for 10 and 11.
12, 13 : If a line has multiple single or double quote enclosed strings,
this will pick the string that is in the given index.
Ex:

*def s = "  'one' some text here 'two' some text here 'three'    "*
*assert s.beSingleQuotes(2) == 'three'*


>> //Method to return the String that is between the nth pair of given
> >> enclosure String
> >> *14. static String bw(String line, String enclosure, int index)*
> >
> > this could be interesting to have
>
> Too special again, imho.
>

Yes. But very handy for non-developers...! Just my opinion !

>
> >
> >> //Method to check whether searchString exists within self String
> >> ignoring
> >> the case
> >> *15. static boolean containsIgnoreCase(String self, String
> >> searchString)*
> >>
> >> //Method to check whether the String starts with the searchString
> >> ignoring
> >> the case
> >> *16. static boolean startsWithIgnoreCase(String self, String
> >> searchString)*
> >>
> >> //Method to check whether the String ends with the searchString
> >> ignoring the
> >> case
> >> *17. static boolean endsWithIgnoreCase(String self, String
> >> searchString)*
> >
> > these could be interesting because they are actually not so easy to
> > get right and efficient in a unicode world
>
> Agree.
>
Ok

>
> Cheers,
> mg
>
>
>
>
>
>

Re: More Default String Methods

Posted by MG <mg...@arscreat.com>.

On 14.10.2018 11:04, Jochen Theodorou wrote:
> On 12.10.2018 21:22, adithyank wrote:
>> As suggested by Jochen Theodorou in this
>> link
>> <http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html> 
>>
>> post, I have created this topic for `More Default String Methods`
>>
>> I would like to work in these if the community accepts this proposal
>>
>> //Method to return left n characters
>> *1. static String left(String self, int chars)*
>
> self[0..chars]?

Having a method here might be useful, but I would call it "fromLeft .
"left" methd to me would be "all the chars left of the given char 
position", which would also be good to have, imho.

>
>> //Method to return right n characters
>> *2. static String right(String self, int chars)*
>
> self[-chars..-1]?

Same as "left" above (with the addition that "-chars-1" is easier to get 
wrong ;-) ).


>
>> //Method to return the String that is after the first occurrence of the
>> given searchString
>> *3. static String after(String self, String searchString)*
>>
>> //Method to return the String that is before the first occurrence of the
>> given searchString
>> *4. static String before(String self, String searchString)*
>
> these sound more interesting... empty String if the search string is 
> not in self?

Asking mysself, how often does anyone need that, and if one does need 
it, would he rememeber/find it with that name ?
Along the same line: Use a regex for special cases like that ?

>
>> //Method to return the String that is before the first '=' character
>> *5. static String getBeforeEq(String self)*
>
> which is foo.before("=") if number 4 is taken... This method looks to 
> me a bit overspecialized

I think it is overspecialized in any case. Better to use a class that 
parses e.g. an properties file for you, or go for a full blown parser here.

>
>> /**
>> Splits the given String with the given separator String and returns the
>> value at the given column Index. By
>> default, successive occurrence of the separator String is taken are 
>> one unit
>> and the split is performed
>> */
>> *6. static String column(String self, String separator, int colIndex)*
>>
>> /**
>> Splits the given String with Space characters and returns the value 
>> at the
>> given column Index.
>> */
>> *7. static String column(String self, int colIndex)*
>
> self.split(speparator)[colIndex]?

Also quite a special method, but depending on implementation, 
performance of "column" method woud be much better.

>
>> //Method to return the String that is between the pair of double quotes
>> *8. static String bwDoubleQuotes(String line)*
>>
>> //Method to return the String that is between the pair of single quotes
>> *9. static String bwSingleQuotes(String line)*
>
> again these look a bit overspecialized to me

unquote(String s, String quoteString = '"') ?

>
>> //Method to return the String that is between the pair of given 
>> enclosure
>> String
>> *10. static String bw(String line, String enclosure)*
>>
>> //Method to return the String that is between the 2 given strings
>> *11. static String bw(String line, String from, String to)*

Is this for parsing, or is it more:
strip(String s, String bracketString) ?
trim(String s, String trimChars) ?

"bw" is way too short / non-obvious a method name in my book.

>>
>>
>> //Method to return the String that is between the nth pair of double 
>> quotes
>> *12. static String bwDoubleQuotes(String line, int index)*
>>
>>
>> //Method to return the String that is between the nth pair of single 
>> quotes
>> *13. static String bwSingleQuotes(String line, int index)*
>
> so 10 is 11 with from=to, 12 is 10 if enclosure " and 13 is 10 with 
> enclosure '. so don`t know about 12 and 13
>
>> //Method to return the String that is between the nth pair of given
>> enclosure String
>> *14. static String bw(String line, String enclosure, int index)*
>
> this could be interesting to have

Too special again, imho.

>
>> //Method to check whether searchString exists within self String 
>> ignoring
>> the case
>> *15. static boolean containsIgnoreCase(String self, String 
>> searchString)*
>>
>> //Method to check whether the String starts with the searchString 
>> ignoring
>> the case
>> *16. static boolean startsWithIgnoreCase(String self, String 
>> searchString)*
>>
>> //Method to check whether the String ends with the searchString 
>> ignoring the
>> case
>> *17. static boolean endsWithIgnoreCase(String self, String 
>> searchString)*
>
> these could be interesting because they are actually not so easy to 
> get right and efficient in a unicode world

Agree.

Cheers,
mg






Re: More Default String Methods

Posted by Jochen Theodorou <bl...@gmx.org>.
On 12.10.2018 21:22, adithyank wrote:
> As suggested by Jochen Theodorou in this
> link
> <http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html>
> post, I have created this topic for `More Default String Methods`
> 
> I would like to work in these if the community accepts this proposal
> 
> //Method to return left n characters
> *1. static String left(String self, int chars)*

self[0..chars]?

> //Method to return right n characters
> *2. static String right(String self, int chars)*

self[-chars..-1]?

> //Method to return the String that is after the first occurrence of the
> given searchString
> *3. static String after(String self, String searchString)*
> 
> //Method to return the String that is before the first occurrence of the
> given searchString
> *4. static String before(String self, String searchString)*

these sound more interesting... empty String if the search string is not 
in self?

> //Method to return the String that is before the first '=' character
> *5. static String getBeforeEq(String self)*

which is foo.before("=") if number 4 is taken... This method looks to me 
a bit overspecialized

> /**
> Splits the given String with the given separator String and returns the
> value at the given column Index. By
> default, successive occurrence of the separator String is taken are one unit
> and the split is performed
> */
> *6. static String column(String self, String separator, int colIndex)*
>
> /**
> Splits the given String with Space characters and returns the value at the
> given column Index.
> */
> *7. static String column(String self, int colIndex)*

self.split(speparator)[colIndex]?

> //Method to return the String that is between the pair of double quotes
> *8. static String bwDoubleQuotes(String line)*
> 
> //Method to return the String that is between the pair of single quotes
> *9. static String bwSingleQuotes(String line)*

again these look a bit overspecialized to me

> //Method to return the String that is between the pair of given enclosure
> String
> *10. static String bw(String line, String enclosure)*
> 
> //Method to return the String that is between the 2 given strings
> *11. static String bw(String line, String from, String to)*
> 
> 
> //Method to return the String that is between the nth pair of double quotes
> *12. static String bwDoubleQuotes(String line, int index)*
> 
> 
> //Method to return the String that is between the nth pair of single quotes
> *13. static String bwSingleQuotes(String line, int index)*

so 10 is 11 with from=to, 12 is 10 if enclosure " and 13 is 10 with 
enclosure '. so don`t know about 12 and 13

> //Method to return the String that is between the nth pair of given
> enclosure String
> *14. static String bw(String line, String enclosure, int index)*

this could be interesting to have

> //Method to check whether searchString exists within self String ignoring
> the case
> *15. static boolean containsIgnoreCase(String self, String searchString)*
> 
> //Method to check whether the String starts with the searchString ignoring
> the case
> *16. static boolean startsWithIgnoreCase(String self, String searchString)*
> 
> //Method to check whether the String ends with the searchString ignoring the
> case
> *17. static boolean endsWithIgnoreCase(String self, String searchString)*

these could be interesting because they are actually not so easy to get 
right and efficient in a unicode world

bye Jochen