Skip to content

Commit b4a2534

Browse files
committed
Deprecate makeValidForR in favour of makeNames
1 parent e6ced4b commit b4a2534

2 files changed

Lines changed: 74 additions & 45 deletions

File tree

src/ubic/basecode/util/StringUtil.java

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ public static boolean isLatinLetter( char c ) {
186186
return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' );
187187
}
188188

189-
private static final String[] R_RESERVED_WORDS = {
190-
"if", "else", "repeat", "while", "function", "for", "in", "next", "break",
191-
"TRUE", "FALSE", "NULL", "Inf", "NaN", "NA", "NA_integer_", "NA_real_", "NA_character_", "NA_complex_",
192-
};
193-
194189
/**
195190
* Mimics the {@code make.names} method in R (character.c) to make valid variables names; we use this for column
196191
* headers in some output files.
@@ -200,8 +195,63 @@ public static boolean isLatinLetter( char c ) {
200195
* @param s a string to be made valid for R
201196
* @return modified string
202197
* @author paul
198+
* @deprecated use {@link #makeNames(String[], boolean)} instead
203199
*/
204200
public static String makeValidForR( String s ) {
201+
return makeNames( s );
202+
}
203+
204+
/**
205+
* Mimics the {@code make.names} method in R when using with a vector of strings and the unique argument set to TRUE.
206+
* @author poirigui
207+
* @deprecated use {@link #makeNames(String[], boolean)} instead
208+
*/
209+
@Deprecated
210+
public static String[] makeValidForR( String[] strings ) {
211+
return makeNames( strings, true );
212+
}
213+
214+
/**
215+
* Mimics the {@code make.names} method in R.
216+
* @param strings a list of strings to be made valid for R
217+
* @param unique if true, will ensure that the names are unique by appending a number to duplicates as per
218+
* {@link #makeUnique(String[])}
219+
* @author poirigui
220+
*/
221+
public static String[] makeNames( String[] strings, boolean unique ) {
222+
String[] result = new String[strings.length];
223+
if ( unique ) {
224+
Map<String, Integer> counts = new HashMap<>();
225+
for ( int i = 0; i < strings.length; i++ ) {
226+
String s = strings[i];
227+
String rs = makeNames( s );
228+
if ( counts.containsKey( rs ) ) {
229+
int count = counts.get( rs );
230+
result[i] = rs + "." + count;
231+
counts.put( rs, count + 1 );
232+
} else {
233+
result[i] = rs;
234+
counts.put( rs, 1 );
235+
}
236+
}
237+
} else {
238+
for ( int i = 0; i < strings.length; i++ ) {
239+
result[i] = makeNames( strings[i] );
240+
}
241+
}
242+
return result;
243+
}
244+
245+
private static final String[] R_RESERVED_WORDS = {
246+
"if", "else", "repeat", "while", "function", "for", "in", "next", "break",
247+
"TRUE", "FALSE", "NULL", "Inf", "NaN", "NA", "NA_integer_", "NA_real_", "NA_character_", "NA_complex_",
248+
};
249+
250+
/**
251+
* Mimics the {@code make.names} method in R for a single string.
252+
* @author paul
253+
*/
254+
public static String makeNames( String s ) {
205255
if ( s == null ) {
206256
return "NA";
207257
}
@@ -218,32 +268,11 @@ public static String makeValidForR( String s ) {
218268
return s.replaceAll( "[^A-Za-z0-9._]", "." );
219269
}
220270

221-
/**
222-
* Mimics the {@code make.names} method in R when using with a vector of strings and the unique argument set to TRUE.
223-
* @author poirigui
224-
*/
225-
public static String[] makeValidForR( String[] strings ) {
226-
String[] result = new String[strings.length];
227-
Map<String, Integer> counts = new HashMap<>();
228-
for ( int i = 0; i < strings.length; i++ ) {
229-
String s = strings[i];
230-
String rs = StringUtil.makeValidForR( s );
231-
if ( counts.containsKey( rs ) ) {
232-
int count = counts.get( rs );
233-
result[i] = rs + "." + count;
234-
counts.put( rs, count + 1 );
235-
} else {
236-
result[i] = rs;
237-
counts.put( rs, 1 );
238-
}
239-
}
240-
return result;
241-
}
242-
243271
/**
244272
* Mimics the {@code make.unique} method in R.
245273
* <p>
246274
* Duplicated values in the input array will be suffixed with a dot and a number, starting from 1.
275+
* @author poirigui
247276
*/
248277
public static String[] makeUnique( String[] strings ) {
249278
Map<String, Integer> counts = new HashMap<>();

test/ubic/basecode/util/StringUtilTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,25 @@ public void testCommonSuffixNone() {
7676
}
7777

7878
@Test
79-
public void testMakeRnames() {
79+
public void testMakeNames() {
8080
assertFalse( Character.isDigit( '.' ) );
81-
assertEquals( "NA", StringUtil.makeValidForR( ( String ) null ) );
82-
assertEquals( "test", StringUtil.makeValidForR( "test" ) );
83-
assertEquals( "X", StringUtil.makeValidForR( "X" ) );
84-
assertEquals( "X123", StringUtil.makeValidForR( "123" ) );
85-
assertEquals( "X", StringUtil.makeValidForR( "" ) );
86-
assertEquals( "X..", StringUtil.makeValidForR( " " ) );
87-
assertEquals( "if.", StringUtil.makeValidForR( "if" ) );
88-
assertEquals( "TRUE.", StringUtil.makeValidForR( "TRUE" ) );
89-
assertEquals( "...", StringUtil.makeValidForR( "..." ) );
90-
assertEquals( "..", StringUtil.makeValidForR( ". " ) );
91-
assertEquals( "X.2way", StringUtil.makeValidForR( ".2way" ) );
92-
assertEquals( "f33oo.dd....f..a", StringUtil.makeValidForR( "f33oo dd . [f] a" ) );
93-
assertEquals( ".f33oo", StringUtil.makeValidForR( ".f33oo" ) );
94-
assertEquals( "...f33oo", StringUtil.makeValidForR( "...f33oo" ) );
95-
assertEquals( "X1foo.dd....f..a", StringUtil.makeValidForR( "1foo dd . [f] a" ) );
96-
assertEquals( "X.1foo.dd....f..a", StringUtil.makeValidForR( ".1foo dd . [f] a" ) );
97-
assertArrayEquals( new String[] { "foo", "foo.1", "foo.2", "bar" }, StringUtil.makeValidForR( new String[] { "foo", "foo", "foo", "bar" } ) );
81+
assertEquals( "NA", StringUtil.makeNames( null ) );
82+
assertEquals( "test", StringUtil.makeNames( "test" ) );
83+
assertEquals( "X", StringUtil.makeNames( "X" ) );
84+
assertEquals( "X123", StringUtil.makeNames( "123" ) );
85+
assertEquals( "X", StringUtil.makeNames( "" ) );
86+
assertEquals( "X..", StringUtil.makeNames( " " ) );
87+
assertEquals( "if.", StringUtil.makeNames( "if" ) );
88+
assertEquals( "TRUE.", StringUtil.makeNames( "TRUE" ) );
89+
assertEquals( "...", StringUtil.makeNames( "..." ) );
90+
assertEquals( "..", StringUtil.makeNames( ". " ) );
91+
assertEquals( "X.2way", StringUtil.makeNames( ".2way" ) );
92+
assertEquals( "f33oo.dd....f..a", StringUtil.makeNames( "f33oo dd . [f] a" ) );
93+
assertEquals( ".f33oo", StringUtil.makeNames( ".f33oo" ) );
94+
assertEquals( "...f33oo", StringUtil.makeNames( "...f33oo" ) );
95+
assertEquals( "X1foo.dd....f..a", StringUtil.makeNames( "1foo dd . [f] a" ) );
96+
assertEquals( "X.1foo.dd....f..a", StringUtil.makeNames( ".1foo dd . [f] a" ) );
97+
assertArrayEquals( new String[] { "foo", "foo.1", "foo.2", "bar" }, StringUtil.makeNames( new String[] { "foo", "foo", "foo", "bar" }, true ) );
9898
}
9999

100100
@Test

0 commit comments

Comments
 (0)