You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Loic Guibert <lf...@yahoo.fr> on 2015/04/22 17:20:01 UTC

[LANG] Contribution adding some features in StringUtils

Hello,
I wrote a personal class "StringHelper" using the StringUtils class and
containing some classic routines.
I think those could be added to the StringUtils class.

There are some of these methods (JavaDoc is actually in French, so I
didn't include it here)  :
--------------------------------------------------------------------------------
    /**
     * StringHelper.abbreviate(null, *, *, *) = null
     * StringHelper.abbreviate("", 10, 2, 3)  = ""
     * StringHelper.abbreviate("0123456789ABCDEF", 16, 3, 2) =
"0123456789ABCDEF"
     * StringHelper.abbreviate("0123456789ABCDEF", 10, 3, 2) = "01234...EF"
     * StringHelper.abbreviate("Lorem ipsum dolor sit amet", 23, 6, 5) =
"Lorem ipsum dol... amet"
     * StringHelper.abbreviate("012345", 5, 0, 2)  => {@link
IllegalArgumentException}
     */
public static String abbreviate(String pString, int pMaxLength, int
pPrefixLength, int pSuffixLength);

    /**
     * StringHelper.abbreviate(null, null, null, *) = null
     * StringHelper.abbreviate("", null, null, *)   = ""
     * StringHelper.abbreviate(null, "", null, *)   = ""
     * StringHelper.abbreviate(null, null, "", *)   = ""
     * StringHelper.abbreviate("", "", "", 10)   = ""
     * StringHelper.abbreviate("a", "b", "c", 10)   = "abc"
     * StringHelper.abbreviate("012", "3456789ABCD", "EF", 16) =
"0123456789ABCDEF"
     * StringHelper.abbreviate("012", "3456789ABCD", "EF", 10) =
"01234...EF"
     * StringHelper.abbreviate("Lorem ", "ipsum dolor sit", " amet", 23)
= "Lorem ipsum dol... amet"
     * StringHelper.abbreviate("", "0123", "45", 5)  => {@link
IllegalArgumentException}
     */
public static String abbreviate(String pPrefix, String pString, String
pSuffix, int pMaxLength);


    /**
     * StringHelper.leftPadLeftCut(null, *, *, false)  = null
     * StringHelper.leftPadLeftCut(null, 3, 'z', true) = "zzz"
     * StringHelper.leftPadLeftCut("", 3, 'z', *)      = "zzz"
     * StringHelper.leftPadLeftCut("bat", 3, 'z', *)   = "bat"
     * StringHelper.leftPadLeftCut("bat", 5, 'z', *)   = "zzbat"
     * StringHelper.leftPadLeftCut("bat", 1, 'z', *)   = "t"
     * StringHelper.leftPadLeftCut("bat", -1, 'z', *)  = ""
     */
public static String leftPadLeftCut(String pString, int pFixedSize, char
pPadChar, boolean pNullAsEmpty);

    /**
     * StringHelper.leftPadRightCut(null, *, *, false)  = null
     * StringHelper.leftPadRightCut(null, 3, 'z', true) = "zzz"
     * StringHelper.leftPadRightCut("", 3, 'z', *)      = "zzz"
     * StringHelper.leftPadRightCut("bat", 3, 'z', *)   = "bat"
     * StringHelper.leftPadRightCut("bat", 5, 'z', *)   = "zzbat"
     * StringHelper.leftPadRightCut("bat", 1, 'z', *)   = "b"
     * StringHelper.leftPadRightCut("bat", -1, 'z', *)  = ""
     */
public static String leftPadRightCut(String pString, int pFixedSize,
char pPadChar, boolean pNullAsEmpty);

    /**
     * StringHelper.rightPadRightCut(null, *, *, false)  = null
     * StringHelper.rightPadRightCut(null, 3, 'z', true) = "zzz"
     * StringHelper.rightPadRightCut("", 3, 'z', *)      = "zzz"
     * StringHelper.rightPadRightCut("bat", 3, 'z', *)   = "bat"
     * StringHelper.rightPadRightCut("bat", 5, 'z', *)   = "batzz"
     * StringHelper.rightPadRightCut("bat", 1, 'z', *)   = "b"
     * StringHelper.rightPadRightCut("bat", -1, 'z', *)  = ""
     */
public static String rightPadRightCut(String pString, int pFixedSize,
char pPadChar, boolean pNullAsEmpty);

    /**
     * StringHelper.rightPadLeftCut(null, *, *, false)  = null
     * StringHelper.rightPadLeftCut(null, 3, 'z', true) = "zzz"
     * StringHelper.rightPadLeftCut("", 3, 'z', *)      = "zzz"
     * StringHelper.rightPadLeftCut("bat", 3, 'z', *)   = "bat"
     * StringHelper.rightPadLeftCut("bat", 5, 'z', *)   = "batzz"
     * StringHelper.rightPadLeftCut("bat", 1, 'z', *)   = "t"
     * StringHelper.rightPadLeftCut("bat", -1, 'z', *)  = ""
     */
public static String rightPadLeftCut(String pString, int pFixedSize,
char pPadChar, boolean pNullAsEmpty);


    /**
     * StringHelper.rotate(null, *)        = null
     * StringHelper.rotate("", *)          = ""
     * StringHelper.rotate("ABCDEFG", 0)   = "ABCDEF"
     * StringHelper.rotate("ABCDEFG", 2)   = "FGABCDE"
     * StringHelper.rotate("ABCDEFG", -2)  = "CDEFGAB"
     * StringHelper.rotate("ABCDEFG", 7)   = "ABCDEFG"
     * StringHelper.rotate("ABCDEFG", -7)  = "ABCDEFG"
     * StringHelper.rotate("ABCDEFG", 9)   = "FGABCDE"
     * StringHelper.rotate("ABCDEFG", -9)  = "CDEFGAB"
     */
public static String rotate(String pString, int pCount);


    /**
     * StringHelper.replaceAll(null, *, *) = null
     * StringHelper.replaceAll(*, null, *)  => {@link NullPointerException}
     * StringHelper.replaceAll(*, *, null)  => {@link NullPointerException}
     * StringHelper.replaceAll("", "", "abc")  = "abc"
     * StringHelper.replaceAll("", ".*", "abc")  = "abc"
     * StringHelper.replaceAll("", ".+", "abc")  = ""
     * StringHelper.replaceAll("ABCabc123", "[a-z]", "_") = "ABC___123"
     * StringHelper.replaceAll("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123"
     * StringHelper.replaceAll("ABCabc123", "[^A-Z0-9]+", "") = "ABC123"
     * StringHelper.replaceAll("Lorem ipsum  dolor   sit", "(
+)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit"
     *
     * @see String#replaceAll(String, String)
     * @see java.util.regex.Pattern
     */
public static String replaceAll(String pString, String pRegex, String
pReplacement);

    /**
     * StringHelper.replaceFirst(null, *, *) = null
     * StringHelper.replaceFirst(*, null, *)  => {@link
NullPointerException}
     * StringHelper.replaceFirst(*, *, null)  => {@link
NullPointerException}
     * StringHelper.replaceFirst("", "", "abc")  = "abc"
     * StringHelper.replaceFirst("", ".*", "abc")  = "abc"
     * StringHelper.replaceFirst("", ".+", "abc")  = ""
     * StringHelper.replaceFirst("ABCabc123", "[a-z]", "_") = "ABC_bc123"
     * StringHelper.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_") =
"ABC_123abc"
     * StringHelper.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "") =
"ABC123abc"
     * StringHelper.replaceFirst("Lorem ipsum  dolor   sit", "(
+)([a-z]+)", "_$2") = "Lorem_ipsum  dolor   sit"
     *
     * @see String#replaceFirst(String, String)
     * @see java.util.regex.Pattern
     */
public static String replaceFirst(String pString, String pRegex, String
pReplacement);


public static int compare(String pString1, String pString2);
public static int compareIgnoreCase(String pString1, String pString2);


    /**
     * @param pAlgorithm : MD5, SHA-1,... see {@link MessageDigest}
     * @throws NoSuchAlgorithmException -
     */
public static String hash(String pString, String pAlgorithm) throws
NoSuchAlgorithmException;
public static byte[] hashBytes(String pString, String pAlgorithm) throws
NoSuchAlgorithmException;


    /**
     * Convert a bytes array to an hexadecimal String represetation (big
endian)
     */
public static String toHexString(byte[] pBytes);


    /**
     * @return Integer or {@code null} if pString is empty
     */
public static Integer toIntegerEmpty(String pString);


    /**
     * StringHelper.splitByLength(null, *) = null
     * StringHelper.splitByLength("abc") = []
     * StringHelper.splitByLength("", 2, 4, 1)     = [null, null, null]
     * StringHelper.splitByLength("abcdefg", 2, 4, 1)  = ["ab", "cdef", "g"]
     * StringHelper.splitByLength("abcdefghij", 2, 4, 1)  = ["ab",
"cdef", "g"]
     * StringHelper.splitByLength("abcdef", 2, 4, 1)  = ["ab", "cdef", null]
     * StringHelper.splitByLength("abcdef ", 2, 4, 1)  = ["ab", "cdef", " "]
     * StringHelper.splitByLength(" abcdefg", 2, 4, 1)  = [" a", "bcde",
"f"]
     * StringHelper.splitByLength("abcdefg", 2, 4, 0, 1)  = ["ab",
"cdef", null, "g"]
     *
     * @return String[] or {@code null} if <code>pString == null</code>
     */
public static String[] splitByLength(String pString, int ... pColLengths);


    /**
     * Return {@link Matcher} from <code>pRegex.matcher(pString)</code>
     * @return {@link Matcher} ou <code>null</code>
     */
public static Matcher matcher(CharSequence pString, Pattern pRegex);

    /**
     * Return {@link Matcher} from
<code>Pattern.compile(pRegex).matcher(pString)</code>
     * @return {@link Matcher} ou <code>null</code>
     */
public static Matcher matcher(CharSequence pString, String pRegex);

public static boolean matches(CharSequence pString, Pattern pRegex);
public static boolean matches(CharSequence pString, String pRegex);
--------------------------------------------------------------------------------

I will rename parameters and add final qualifier to respect your coding
practices.
I will translate the JavaDoc and comments in English.

If you are interested, I can add those to StringUtils (and associated
test cases) in my github repository.
It's my first contribution, so how would you like it to be done ?
    1- make a JIRA ticket by logical group of methods and related git
pull request
    2- make a single detailed JIRA ticket with all methods and a single
git pull request with one commit by logical group of methods



Best regards,

-- 
Loic Guibert



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [LANG] Contribution adding some features in StringUtils

Posted by Benedikt Ritter <br...@apache.org>.
Hello Loic,

welcome to the dev list!

2015-04-22 17:20 GMT+02:00 Loic Guibert <lf...@yahoo.fr>:

> Hello,
> I wrote a personal class "StringHelper" using the StringUtils class and
> containing some classic routines.
> I think those could be added to the StringUtils class.
>

Nice that you've decided to contribute to the OSS ecosystem!


>
> There are some of these methods (JavaDoc is actually in French, so I
> didn't include it here)  :
>
> --------------------------------------------------------------------------------
>     /**
>      * StringHelper.abbreviate(null, *, *, *) = null
>      * StringHelper.abbreviate("", 10, 2, 3)  = ""
>      * StringHelper.abbreviate("0123456789ABCDEF", 16, 3, 2) =
> "0123456789ABCDEF"
>      * StringHelper.abbreviate("0123456789ABCDEF", 10, 3, 2) = "01234...EF"
>      * StringHelper.abbreviate("Lorem ipsum dolor sit amet", 23, 6, 5) =
> "Lorem ipsum dol... amet"
>      * StringHelper.abbreviate("012345", 5, 0, 2)  => {@link
> IllegalArgumentException}
>      */
> public static String abbreviate(String pString, int pMaxLength, int
> pPrefixLength, int pSuffixLength);
>
>     /**
>      * StringHelper.abbreviate(null, null, null, *) = null
>      * StringHelper.abbreviate("", null, null, *)   = ""
>      * StringHelper.abbreviate(null, "", null, *)   = ""
>      * StringHelper.abbreviate(null, null, "", *)   = ""
>      * StringHelper.abbreviate("", "", "", 10)   = ""
>      * StringHelper.abbreviate("a", "b", "c", 10)   = "abc"
>      * StringHelper.abbreviate("012", "3456789ABCD", "EF", 16) =
> "0123456789ABCDEF"
>      * StringHelper.abbreviate("012", "3456789ABCD", "EF", 10) =
> "01234...EF"
>      * StringHelper.abbreviate("Lorem ", "ipsum dolor sit", " amet", 23)
> = "Lorem ipsum dol... amet"
>      * StringHelper.abbreviate("", "0123", "45", 5)  => {@link
> IllegalArgumentException}
>      */
> public static String abbreviate(String pPrefix, String pString, String
> pSuffix, int pMaxLength);
>
>
>     /**
>      * StringHelper.leftPadLeftCut(null, *, *, false)  = null
>      * StringHelper.leftPadLeftCut(null, 3, 'z', true) = "zzz"
>      * StringHelper.leftPadLeftCut("", 3, 'z', *)      = "zzz"
>      * StringHelper.leftPadLeftCut("bat", 3, 'z', *)   = "bat"
>      * StringHelper.leftPadLeftCut("bat", 5, 'z', *)   = "zzbat"
>      * StringHelper.leftPadLeftCut("bat", 1, 'z', *)   = "t"
>      * StringHelper.leftPadLeftCut("bat", -1, 'z', *)  = ""
>      */
> public static String leftPadLeftCut(String pString, int pFixedSize, char
> pPadChar, boolean pNullAsEmpty);
>
>     /**
>      * StringHelper.leftPadRightCut(null, *, *, false)  = null
>      * StringHelper.leftPadRightCut(null, 3, 'z', true) = "zzz"
>      * StringHelper.leftPadRightCut("", 3, 'z', *)      = "zzz"
>      * StringHelper.leftPadRightCut("bat", 3, 'z', *)   = "bat"
>      * StringHelper.leftPadRightCut("bat", 5, 'z', *)   = "zzbat"
>      * StringHelper.leftPadRightCut("bat", 1, 'z', *)   = "b"
>      * StringHelper.leftPadRightCut("bat", -1, 'z', *)  = ""
>      */
> public static String leftPadRightCut(String pString, int pFixedSize,
> char pPadChar, boolean pNullAsEmpty);
>
>     /**
>      * StringHelper.rightPadRightCut(null, *, *, false)  = null
>      * StringHelper.rightPadRightCut(null, 3, 'z', true) = "zzz"
>      * StringHelper.rightPadRightCut("", 3, 'z', *)      = "zzz"
>      * StringHelper.rightPadRightCut("bat", 3, 'z', *)   = "bat"
>      * StringHelper.rightPadRightCut("bat", 5, 'z', *)   = "batzz"
>      * StringHelper.rightPadRightCut("bat", 1, 'z', *)   = "b"
>      * StringHelper.rightPadRightCut("bat", -1, 'z', *)  = ""
>      */
> public static String rightPadRightCut(String pString, int pFixedSize,
> char pPadChar, boolean pNullAsEmpty);
>
>     /**
>      * StringHelper.rightPadLeftCut(null, *, *, false)  = null
>      * StringHelper.rightPadLeftCut(null, 3, 'z', true) = "zzz"
>      * StringHelper.rightPadLeftCut("", 3, 'z', *)      = "zzz"
>      * StringHelper.rightPadLeftCut("bat", 3, 'z', *)   = "bat"
>      * StringHelper.rightPadLeftCut("bat", 5, 'z', *)   = "batzz"
>      * StringHelper.rightPadLeftCut("bat", 1, 'z', *)   = "t"
>      * StringHelper.rightPadLeftCut("bat", -1, 'z', *)  = ""
>      */
> public static String rightPadLeftCut(String pString, int pFixedSize,
> char pPadChar, boolean pNullAsEmpty);
>
>
>     /**
>      * StringHelper.rotate(null, *)        = null
>      * StringHelper.rotate("", *)          = ""
>      * StringHelper.rotate("ABCDEFG", 0)   = "ABCDEF"
>      * StringHelper.rotate("ABCDEFG", 2)   = "FGABCDE"
>      * StringHelper.rotate("ABCDEFG", -2)  = "CDEFGAB"
>      * StringHelper.rotate("ABCDEFG", 7)   = "ABCDEFG"
>      * StringHelper.rotate("ABCDEFG", -7)  = "ABCDEFG"
>      * StringHelper.rotate("ABCDEFG", 9)   = "FGABCDE"
>      * StringHelper.rotate("ABCDEFG", -9)  = "CDEFGAB"
>      */
> public static String rotate(String pString, int pCount);
>
>
>     /**
>      * StringHelper.replaceAll(null, *, *) = null
>      * StringHelper.replaceAll(*, null, *)  => {@link NullPointerException}
>      * StringHelper.replaceAll(*, *, null)  => {@link NullPointerException}
>      * StringHelper.replaceAll("", "", "abc")  = "abc"
>      * StringHelper.replaceAll("", ".*", "abc")  = "abc"
>      * StringHelper.replaceAll("", ".+", "abc")  = ""
>      * StringHelper.replaceAll("ABCabc123", "[a-z]", "_") = "ABC___123"
>      * StringHelper.replaceAll("ABCabc123", "[^A-Z0-9]+", "_") = "ABC_123"
>      * StringHelper.replaceAll("ABCabc123", "[^A-Z0-9]+", "") = "ABC123"
>      * StringHelper.replaceAll("Lorem ipsum  dolor   sit", "(
> +)([a-z]+)", "_$2") = "Lorem_ipsum_dolor_sit"
>      *
>      * @see String#replaceAll(String, String)
>      * @see java.util.regex.Pattern
>      */
> public static String replaceAll(String pString, String pRegex, String
> pReplacement);
>
>     /**
>      * StringHelper.replaceFirst(null, *, *) = null
>      * StringHelper.replaceFirst(*, null, *)  => {@link
> NullPointerException}
>      * StringHelper.replaceFirst(*, *, null)  => {@link
> NullPointerException}
>      * StringHelper.replaceFirst("", "", "abc")  = "abc"
>      * StringHelper.replaceFirst("", ".*", "abc")  = "abc"
>      * StringHelper.replaceFirst("", ".+", "abc")  = ""
>      * StringHelper.replaceFirst("ABCabc123", "[a-z]", "_") = "ABC_bc123"
>      * StringHelper.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "_") =
> "ABC_123abc"
>      * StringHelper.replaceFirst("ABCabc123abc", "[^A-Z0-9]+", "") =
> "ABC123abc"
>      * StringHelper.replaceFirst("Lorem ipsum  dolor   sit", "(
> +)([a-z]+)", "_$2") = "Lorem_ipsum  dolor   sit"
>      *
>      * @see String#replaceFirst(String, String)
>      * @see java.util.regex.Pattern
>      */
> public static String replaceFirst(String pString, String pRegex, String
> pReplacement);
>
>
> public static int compare(String pString1, String pString2);
> public static int compareIgnoreCase(String pString1, String pString2);
>
>
>     /**
>      * @param pAlgorithm : MD5, SHA-1,... see {@link MessageDigest}
>      * @throws NoSuchAlgorithmException -
>      */
> public static String hash(String pString, String pAlgorithm) throws
> NoSuchAlgorithmException;
> public static byte[] hashBytes(String pString, String pAlgorithm) throws
> NoSuchAlgorithmException;
>
>
>     /**
>      * Convert a bytes array to an hexadecimal String represetation (big
> endian)
>      */
> public static String toHexString(byte[] pBytes);
>
>
>     /**
>      * @return Integer or {@code null} if pString is empty
>      */
> public static Integer toIntegerEmpty(String pString);
>
>
>     /**
>      * StringHelper.splitByLength(null, *) = null
>      * StringHelper.splitByLength("abc") = []
>      * StringHelper.splitByLength("", 2, 4, 1)     = [null, null, null]
>      * StringHelper.splitByLength("abcdefg", 2, 4, 1)  = ["ab", "cdef",
> "g"]
>      * StringHelper.splitByLength("abcdefghij", 2, 4, 1)  = ["ab",
> "cdef", "g"]
>      * StringHelper.splitByLength("abcdef", 2, 4, 1)  = ["ab", "cdef",
> null]
>      * StringHelper.splitByLength("abcdef ", 2, 4, 1)  = ["ab", "cdef", "
> "]
>      * StringHelper.splitByLength(" abcdefg", 2, 4, 1)  = [" a", "bcde",
> "f"]
>      * StringHelper.splitByLength("abcdefg", 2, 4, 0, 1)  = ["ab",
> "cdef", null, "g"]
>      *
>      * @return String[] or {@code null} if <code>pString == null</code>
>      */
> public static String[] splitByLength(String pString, int ... pColLengths);
>
>
>     /**
>      * Return {@link Matcher} from <code>pRegex.matcher(pString)</code>
>      * @return {@link Matcher} ou <code>null</code>
>      */
> public static Matcher matcher(CharSequence pString, Pattern pRegex);
>
>     /**
>      * Return {@link Matcher} from
> <code>Pattern.compile(pRegex).matcher(pString)</code>
>      * @return {@link Matcher} ou <code>null</code>
>      */
> public static Matcher matcher(CharSequence pString, String pRegex);
>
> public static boolean matches(CharSequence pString, Pattern pRegex);
> public static boolean matches(CharSequence pString, String pRegex);
>
> --------------------------------------------------------------------------------
>
> I will rename parameters and add final qualifier to respect your coding
> practices.
> I will translate the JavaDoc and comments in English.
>
> If you are interested, I can add those to StringUtils (and associated
> test cases) in my github repository.
>

The functionality you propose looks generally useful to me. However, I'm
not sure if everything fits into StringUtils.


> It's my first contribution, so how would you like it to be done ?
>

The first step is to sign a Contributor License Agreement [1]. It's not
totally necessary for one time contributions, but it's good to have this
kind of paper work done, if you decide to stay with us for longer ;o)


>     1- make a JIRA ticket by logical group of methods and related git
> pull request

    2- make a single detailed JIRA ticket with all methods and a single
> git pull request with one commit by logical group of methods
>
>
Using GitHub for contributing is fine. Please read through our docs on how
to contribute [2]. It will make applying your contribution easier.
Also creating a PR per functional group will be better.


Thanks!
Benedikt

[1] https://www.apache.org/dev/new-committers-guide.html#cla
[2] https://github.com/apache/commons-lang/blob/trunk/CONTRIBUTING.md


>
> Best regards,
>
> --
> Loic Guibert
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>


-- 
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter