StringUtils { public static boolean codeAwareEqualsIgnoreSpaces(String in1, String in2) { if ( in1 == null || in2 == null ) { return in1 == null && in2 == null; } if ( in1.isEmpty() && in2.isEmpty() ) { return true; } if ( in1.length() == 0 ) { in1 = " "; } if ( in2.length() == 0 ) { in2 = " "; } int idx1 = 0; Character quoted1 = null; int idx2 = 0; Character quoted2 = null; boolean equals = true; while ( equals ) { while ( idx1 < (in1.length()) && (quoted1==null) && isWhitespace(in1.charAt(idx1)) ) { idx1++; } while ( idx2 < (in2.length()) && (quoted2==null) && isWhitespace(in2.charAt(idx2)) ) { idx2++; } if ( idx1 >= in1.length() || idx2 >= in2.length() ) { equals = equals && idx1 == in1.length() && idx2 == in2.length(); break; } if ( in1.charAt(idx1) == '"' || in1.charAt(idx1) == '\'' ) { if ( quoted1 == null ) { quoted1 = in1.charAt(idx1); } else if ( quoted1 != null && quoted1.equals( in1.charAt(idx1) ) ) { if ( in1.charAt(idx1-1) != '\\' ) { quoted1 = null; } } } if ( in2.charAt(idx2) == '"' || in2.charAt(idx2) == '\'' ) { if ( quoted2 == null ) { quoted2 = in2.charAt(idx2); } else if ( quoted2 != null && quoted2.equals( in2.charAt(idx2) ) ) { if ( in2.charAt(idx2-1) != '\\' ) { quoted2 = null; } } } equals &= in1.charAt(idx1) == in2.charAt(idx2); idx1++; idx2++; } return equals; } StringUtils(); static String ucFirst(final String s); static String lcFirst(String s); static String lcFirstForBean(String s); static boolean isEmpty(final CharSequence str); static String repeat(final String str,
                                final int repeat); static String[] splitPreserveAllTokens(final String str,
                                                  final String separatorChars); static String padding(final int repeat,
                                 final char padChar); static String readFileAsString(Reader reader); static String unescapeJava(String str); static void unescapeJava(Writer out, String str); static String cleanPath(String path); static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix); static String collectionToDelimitedString(Collection coll, String delim); static String replace(String inString, String oldPattern, String newPattern); static URI toURI(String location); static String escapeXmlString(String string); static String[] delimitedListToStringArray(String str, String delimiter); static String[] delimitedListToStringArray(String str, String delimiter, String charsToDelete); static String[] toStringArray(Collection collection); static String deleteAny(String inString, String charsToDelete); static String toString(Reader reader); static String toString(InputStream is); static String toString(BufferedReader reader); static String getPkgUUID(ReleaseId releaseId, String packageName); static String getPkgUUID(String gav, String packageName); static String md5Hash(String s); static String bytesToHex(byte[] bytes); static String generateUUID(); static String extractFirstIdentifier(String string, int start); static int extractFirstIdentifier(String string, StringBuilder builder, int start); static int skipBlanks(String string, int start); static List<String> splitStatements(CharSequence string); static List<String> splitArgumentsList(CharSequence string); static List<String> splitArgumentsList(CharSequence string, boolean trimArgs); static boolean codeAwareEqualsIgnoreSpaces(String in1, String in2); static int findEndOfMethodArgsIndex(CharSequence string, int startOfMethodArgsIndex); static int indexOfOutOfQuotes(String str, String searched); static int indexOfOutOfQuotes(String str, String searched, int fromIndex); static int codeAwareIndexOf(String str, String searched); static int codeAwareIndexOf(String str, String searched, int fromIndex); static String replaceOutOfQuotes( String s, String oldValue, String newValue ); static int indexOfOutOfQuotes(String str, char searched); static boolean isIdentifier(String expr); static boolean isDereferencingIdentifier(String expr); static double stringSimilarity( String s1, String s2, SIMILARITY_STRATS method ); static boolean equalsIgnoreSpaces(String s1, String s2); static String uuid(); static final String[] EMPTY_STRING_ARRAY; static final String EMPTY; static final int INDEX_NOT_FOUND; }
@Test public void test_codeAwareEqualsIgnoreSpaces() { Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( null, null ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "", "") ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "", null ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( null, "" ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( " ", "" ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "", " " ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( " ", " " ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "rule Rx when then end", " rule Rx when then end " ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "rule Rx when then end\n", " rule Rx when then end\n " ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n", "package org.drools.compiler\n " + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n", "package org.drools.compiler\n " + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n", " package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n", " package org.drools.compiler\n " + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n " ) ); Assert.assertTrue( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello' World\" )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == \"Hello' World\" )\n" + "then\n" + "end\n" ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello World' )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello World' )\n" + "then\n" + "end\n" ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello\" World' )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello\" World' )\n" + "then\n" + "end\n" ) ); Assert.assertFalse( StringUtils.codeAwareEqualsIgnoreSpaces( "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello\\' World' )\n" + "then\n" + "end\n", "package org.drools.compiler\n" + "rule Rx when\n" + " $m : Message( message == 'Hello\\' World' )\n" + "then\n" + "end\n" ) ); }