Skip to content

Commit 59fdc0b

Browse files
author
kristina
committed
flag changes
1 parent 246c691 commit 59fdc0b

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/main/com/mongodb/Bytes.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.nio.*;
66
import java.nio.charset.*;
77
import java.util.regex.Pattern;
8-
import java.util.Arrays;
8+
import java.util.*;
99

1010
/**
1111
* <type><name>0<data>
@@ -42,6 +42,8 @@ public class Bytes {
4242
static final byte SYMBOL = 14;
4343
static final byte CODE_W_SCOPE = 15;
4444
static final byte NUMBER_INT = 16;
45+
46+
private static final int GLOBAL_FLAG = 256;
4547

4648

4749
/*
@@ -109,22 +111,19 @@ public static boolean cameFromDB( DBObject o ){
109111

110112
public static int patternFlags( String flags ){
111113
flags = flags.toLowerCase();
112-
Flag f[] = Flag.values();
113114
int fint = 0;
114-
int count = 0;
115115

116-
for( Flag flag : f ) {
117-
if( flags.indexOf( flag.flagChar ) >= 0 ) {
116+
for( int i=0; i<flags.length(); i++ ) {
117+
Flag flag = Flag.getByCharacter( flags.charAt( i ) );
118+
if( flag != null ) {
118119
fint |= flag.javaFlag;
119-
count++;
120120
if( flag.unsupported != null )
121121
_warnUnsupported( flag.unsupported );
122122
}
123+
else {
124+
throw new RuntimeException( "unrecognized flag: "+flags.charAt( i ) );
125+
}
123126
}
124-
125-
if( count < flags.length() )
126-
throw new RuntimeException( "some flags could not be converted: "+flags );
127-
128127
return fint;
129128
}
130129

@@ -149,14 +148,26 @@ public static String patternFlags( int flags ){
149148

150149
private static enum Flag {
151150
CANON_EQ( Pattern.CANON_EQ, 'c', "PATTERN.CANON_EQ" ),
152-
DOTALL( Pattern.DOTALL, 'd', "Pattern.DOTALL" ),
151+
UNIX_LINES(Pattern.UNIX_LINES, 'd', "Pattern.UNIX_LINES" ),
152+
GLOBAL( GLOBAL_FLAG, 'g', null ),
153153
CASE_INSENSITIVE( Pattern.CASE_INSENSITIVE, 'i', null ),
154-
UNIX_LINES( Pattern.UNIX_LINES, 'l', "Pattern.UNIX_LINES" ),
155154
MULTILINE(Pattern.MULTILINE, 'm', null ),
155+
DOTALL( Pattern.DOTALL, 's', "Pattern.DOTALL" ),
156156
LITERAL( Pattern.LITERAL, 't', "Pattern.LITERAL" ),
157157
UNICODE_CASE( Pattern.UNICODE_CASE, 'u', "Pattern.UNICODE_CASE" ),
158158
COMMENTS( Pattern.COMMENTS, 'x', null );
159159

160+
private static final Map<Character, Flag> byCharacter = new HashMap<Character, Flag>();
161+
162+
static {
163+
for (Flag flag : values()) {
164+
byCharacter.put(flag.flagChar, flag);
165+
}
166+
}
167+
168+
public static Flag getByCharacter(char ch) {
169+
return byCharacter.get(ch);
170+
}
160171
public final int javaFlag;
161172
public final char flagChar;
162173
public final String unsupported;

src/test/com/mongodb/ByteTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public void testPatternFlags() {
346346
threw = false;
347347

348348
try {
349-
Bytes.patternFlags( 257 );
349+
Bytes.patternFlags( 513 );
350350
}
351351
catch( RuntimeException e ) {
352352
threw = true;
@@ -360,7 +360,8 @@ public void testPatternFlags() {
360360
Pattern.MULTILINE &
361361
Pattern.LITERAL &
362362
Pattern.UNICODE_CASE &
363-
Pattern.COMMENTS );
363+
Pattern.COMMENTS &
364+
256 );
364365

365366
int check = Bytes.patternFlags( Bytes.patternFlags( lotsoflags.flags() ) );
366367
assertEquals( lotsoflags.flags(), check );

0 commit comments

Comments
 (0)