Style bits on wi-fi code
[zxing.git] / android / src / com / google / zxing / client / android / wifi / NetworkUtil.java
index fe15108..6463435 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.google.zxing.client.android.wifi;
 
+import java.util.regex.Pattern;
+
 import android.text.TextUtils;
 
 /**
@@ -28,6 +30,8 @@ import android.text.TextUtils;
  */
 final class NetworkUtil {
 
+  private static final Pattern HEX_DIGITS = Pattern.compile("[0-9A-Fa-f]+");
+
   private NetworkUtil() {
   }
 
@@ -43,26 +47,13 @@ final class NetworkUtil {
     if (TextUtils.isEmpty(string)) {
       return "";
     }
-    final int lastPos = string.length() - 1;
+    int lastPos = string.length() - 1;
     if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
       return string;
     }
     return '\"' + string + '\"';
   }
 
-  private static boolean isHex(CharSequence key) {
-    if (key == null){
-      return false;
-    }
-    for (int i = key.length() - 1; i >= 0; i--) {
-      final char c = key.charAt(i);
-      if (!(c >= '0' && c <= '9' || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f')) {
-        return false;
-      }
-    }
-    return true;
-  }
-
   /**
    * Check if wepKey is a valid hexadecimal string.
    * @param wepKey the input to be checked
@@ -72,9 +63,9 @@ final class NetworkUtil {
     if (wepKey == null) {
       return false;
     }
-    final int length = wepKey.length();
+    int length = wepKey.length();
     // WEP-40, WEP-104, and some vendors using 256-bit WEP (WEP-232?)
-    return (length == 10 || length == 26 || length == 58) && isHex(wepKey);
+    return (length == 10 || length == 26 || length == 58) && HEX_DIGITS.matcher(wepKey).matches();
   }
 
 }
\ No newline at end of file