From 02a98515a337991f0ce05271ac90121fa204d9db Mon Sep 17 00:00:00 2001 From: Loic Guibert Date: Sun, 26 Apr 2015 16:57:21 +0400 Subject: [PATCH] LANG-1119: Add StringUtils.rotate(String, int) --- .../org/apache/commons/lang3/StringUtils.java | 46 +++++++++++++++++++ .../apache/commons/lang3/StringUtilsTest.java | 16 +++++++ 2 files changed, 62 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 26245726d..279c472b9 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -74,6 +74,8 @@ import java.util.regex.Pattern; * - checks the characters in a String *
  • DefaultString * - protects against a null input String
  • + *
  • Rotate + * - rotate (circular shift) a String
  • *
  • Reverse/ReverseDelimited * - reverses a String
  • *
  • Abbreviate @@ -6338,6 +6340,50 @@ public class StringUtils { return isEmpty(str) ? defaultStr : str; } + // Rotating (circular shift) + //----------------------------------------------------------------------- + /** + *

    Rotate (circular shift) a String of {@code shift} characters.

    + * + * + *
    +     * StringUtils.rotate(null, *)        = null
    +     * StringUtils.rotate("", *)          = ""
    +     * StringUtils.rotate("abcdefg", 0)   = "abcdefg"
    +     * StringUtils.rotate("abcdefg", 2)   = "fgabcde"
    +     * StringUtils.rotate("abcdefg", -2)  = "cdefgab"
    +     * StringUtils.rotate("abcdefg", 7)   = "abcdefg"
    +     * StringUtils.rotate("abcdefg", -7)  = "abcdefg"
    +     * StringUtils.rotate("abcdefg", 9)   = "fgabcde"
    +     * StringUtils.rotate("abcdefg", -9)  = "cdefgab"
    +     * 
    + * + * @param str the String to rotate, may be null + * @param shift number of time to shift (positive : right shift, negative : left shift) + * @return the rotated String, + * or the original String if {@code shift == 0}, + * or {@code null} if null String input + */ + public static String rotate(String str, int shift) { + if (str == null) { + return null; + } + + final int strLen = str.length(); + if (shift == 0 || strLen == 0 || shift % strLen == 0) { + return str; + } + + final StringBuilder builder = new StringBuilder(strLen); + final int offset = - (shift % strLen); + builder.append(substring(str, offset)); + builder.append(substring(str, 0, offset)); + return builder.toString(); + } + // Reversing //----------------------------------------------------------------------- /** diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 3addd78a2..f11ec703d 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1625,6 +1625,22 @@ public class StringUtilsTest { assertEquals(" abc ", StringUtils.center("abc", 7, "")); } + //----------------------------------------------------------------------- + @Test + public void testRotate_StringInt() { + assertEquals(null, StringUtils.rotate(null, 1)); + assertEquals("", StringUtils.rotate("", 1)); + assertEquals("abcdefg", StringUtils.rotate("abcdefg", 0)); + assertEquals("fgabcde", StringUtils.rotate("abcdefg", 2)); + assertEquals("cdefgab", StringUtils.rotate("abcdefg", -2)); + assertEquals("abcdefg", StringUtils.rotate("abcdefg", 7)); + assertEquals("abcdefg", StringUtils.rotate("abcdefg", -7)); + assertEquals("fgabcde", StringUtils.rotate("abcdefg", 9)); + assertEquals("cdefgab", StringUtils.rotate("abcdefg", -9)); + assertEquals("efgabcd", StringUtils.rotate("abcdefg", 17)); + assertEquals("defgabc", StringUtils.rotate("abcdefg", -17)); + } + //----------------------------------------------------------------------- @Test public void testReverse_String() {