Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.distiller; | 5 package org.chromium.distiller; |
| 6 | 6 |
| 7 import com.google.gwt.regexp.shared.RegExp; | 7 import com.google.gwt.regexp.shared.RegExp; |
| 8 | 8 |
| 9 public class StringUtil { | 9 public class StringUtil { |
| 10 // For the whitespace-related functions below, Java's and Javascript's versi ons of '\s' and '\S' | 10 // For the whitespace-related functions below, Java's and Javascript's versi ons of '\s' and '\S' |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 } | 35 } |
| 36 | 36 |
| 37 public static boolean match(String input, String regex) { | 37 public static boolean match(String input, String regex) { |
| 38 return RegExp.compile(regex, "i").test(input); | 38 return RegExp.compile(regex, "i").test(input); |
| 39 } | 39 } |
| 40 | 40 |
| 41 public static String findAndReplace(String input, String regex, String repla ce) { | 41 public static String findAndReplace(String input, String regex, String repla ce) { |
| 42 return RegExp.compile(regex, "gi").replace(input, replace); | 42 return RegExp.compile(regex, "gi").replace(input, replace); |
| 43 } | 43 } |
| 44 | 44 |
| 45 public static native boolean containsWordCharacter(String s) /*-{ | 45 /** |
| 46 return /[\w\u00C0-\u1FFF\u2C00-\uD7FF]/.test(s); | 46 * For some languages, counting the number of words relies on non-trivial wo rd |
| 47 * segmentation algorithms, or even huge look-up tables. This function needs to | |
| 48 * be reasonably fast, so the word count for some languages would only be an | |
| 49 * approximation. | |
| 50 * Read https://crbug.com/484750 for more info. | |
| 51 */ | |
| 52 | |
| 53 // Placeholder for the local function variable. The type is wrong. | |
| 54 public static int _countWords; | |
|
cjhopman
2015/05/26 21:53:06
Can't this be a JavaScriptObject? Those can store
wychen
2015/05/29 07:44:09
Done the real Java way.
| |
| 55 | |
| 56 public static native void selectCountWordsFunc(String text) /*-{ | |
| 57 if (/[\u3040-\uA4CF]/.test(text)) { | |
| 58 @StringUtil::_countWords = function(s) { | |
| 59 // The following range includes broader alphabetical letters and Hangul Syllables. | |
| 60 var m = s.match(/(\S*[\w\u00C0-\u1FFF\uAC00-\uD7AF]\S*)/g); | |
| 61 var c = (m ? m.length : 0); | |
| 62 // The following range includes Hiragana, Katakana, and CJK Unif ied Ideographs. | |
| 63 // Hangul Syllables are not included. | |
| 64 m = s.match(/([\u3040-\uA4CF])/g); | |
| 65 c += Math.ceil((m ? m.length : 0) * 0.55); | |
| 66 return c; | |
| 67 }; | |
| 68 return; | |
| 69 } | |
| 70 if (/[\AC00-\uD7AF]/.test(text)) { | |
| 71 @StringUtil::_countWords = function(s) { | |
| 72 // The following range includes broader alphabetical letters and Hangul Syllables. | |
| 73 var m = s.match(/(\S*[\w\u00C0-\u1FFF\uAC00-\uD7AF]\S*)/g); | |
| 74 return (m ? m.length : 0); | |
| 75 }; | |
| 76 return; | |
| 77 } | |
| 78 @StringUtil::_countWords = function(s) { | |
| 79 // The following range includes broader alphabetical letters. | |
| 80 var m = s.match(/(\S*[\w\u00C0-\u1FFF]\S*)/g); | |
| 81 return (m ? m.length : 0); | |
| 82 }; | |
| 47 }-*/; | 83 }-*/; |
| 48 | 84 |
| 85 // Use the safest version of countWords as the default. | |
| 86 private static final int __dummy_initCountWords = initCountWords(); | |
| 87 private static int initCountWords() { | |
| 88 selectCountWordsFunc("字"); | |
| 89 return 0; | |
| 90 } | |
| 91 | |
| 49 public static native int countWords(String s) /*-{ | 92 public static native int countWords(String s) /*-{ |
| 50 var m = s.match(/(\S*[\w\u00C0-\u1FFF\u2C00-\uD7FF]\S*)/g); | 93 // Use a different variable to get around the type checking. |
| 51 return m ? m.length : 0; | 94 var func = @StringUtil::_countWords; |
| 95 return func(s); | |
| 52 }-*/; | 96 }-*/; |
| 53 | 97 |
| 54 public static native String regexEscape(String s) /*-{ | 98 public static native String regexEscape(String s) /*-{ |
| 55 return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); | 99 return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); |
| 56 }-*/; | 100 }-*/; |
| 57 | 101 |
| 58 /* | 102 /* |
| 59 * Returns true if character is a digit. | 103 * Returns true if character is a digit. |
| 60 */ | 104 */ |
| 61 public static native boolean isDigit(Character c) /*-{ | 105 public static native boolean isDigit(Character c) /*-{ |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 79 /** | 123 /** |
| 80 * Returns the plain number if given string can be converted to one >= 0. | 124 * Returns the plain number if given string can be converted to one >= 0. |
| 81 * Returns -1 if string is empty or not all digits. | 125 * Returns -1 if string is empty or not all digits. |
| 82 */ | 126 */ |
| 83 public static int toNumber(String s) { | 127 public static int toNumber(String s) { |
| 84 if (s.isEmpty() || !StringUtil.isStringAllDigits(s)) return -1; | 128 if (s.isEmpty() || !StringUtil.isStringAllDigits(s)) return -1; |
| 85 return JavaScript.parseInt(s, 10); | 129 return JavaScript.parseInt(s, 10); |
| 86 } | 130 } |
| 87 | 131 |
| 88 } | 132 } |
| OLD | NEW |