⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 矩阵的QR分解算法
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	text.append(options[i] + ' ');      }    }    if (illegalOptionsFound > 0) {      throw new Exception("Illegal options: " + text);    }  }    /**   * Checks if the given array contains the flag "-Char". Stops   * searching at the first marker "--". If the flag is found,   * it is replaced with the empty string.   *   * @param flag the character indicating the flag.   * @param options the array of strings containing all the options.   * @return true if the flag was found   * @exception Exception if an illegal option was found   */  public static boolean getFlag(char flag, String[] options)     throws Exception {        return getFlag("" + flag, options);  }    /**   * Checks if the given array contains the flag "-String". Stops   * searching at the first marker "--". If the flag is found,   * it is replaced with the empty string.   *   * @param flag the String indicating the flag.   * @param options the array of strings containing all the options.   * @return true if the flag was found   * @exception Exception if an illegal option was found   */  public static boolean getFlag(String flag, String[] options)     throws Exception {        int pos = getOptionPos(flag, options);    if (pos > -1)      options[pos] = "";        return (pos > -1);  }  /**   * Gets an option indicated by a flag "-Char" from the given array   * of strings. Stops searching at the first marker "--". Replaces    * flag and option with empty strings.   *   * @param flag the character indicating the option.   * @param options the array of strings containing all the options.   * @return the indicated option or an empty string   * @exception Exception if the option indicated by the flag can't be found   */  public static /*@non_null@*/ String getOption(char flag, String[] options)     throws Exception {        return getOption("" + flag, options);  }  /**   * Gets an option indicated by a flag "-String" from the given array   * of strings. Stops searching at the first marker "--". Replaces    * flag and option with empty strings.   *   * @param flag the String indicating the option.   * @param options the array of strings containing all the options.   * @return the indicated option or an empty string   * @exception Exception if the option indicated by the flag can't be found   */  public static /*@non_null@*/ String getOption(String flag, String[] options)     throws Exception {    String newString;    int i = getOptionPos(flag, options);    if (i > -1) {      if (options[i].equals("-" + flag)) {	if (i + 1 == options.length) {	  throw new Exception("No value given for -" + flag + " option.");	}	options[i] = "";	newString = new String(options[i + 1]);	options[i + 1] = "";	return newString;      }      if (options[i].charAt(1) == '-') {	return "";      }    }        return "";  }  /**   * Gets the index of an option or flag indicated by a flag "-Char" from    * the given array of strings. Stops searching at the first marker "--".   *   * @param flag 	the character indicating the option.   * @param options 	the array of strings containing all the options.   * @return 		the position if found, or -1 otherwise   */  public static int getOptionPos(char flag, String[] options) {     return getOptionPos("" + flag, options);  }  /**   * Gets the index of an option or flag indicated by a flag "-String" from    * the given array of strings. Stops searching at the first marker "--".   *   * @param flag 	the String indicating the option.   * @param options 	the array of strings containing all the options.   * @return 		the position if found, or -1 otherwise   */  public static int getOptionPos(String flag, String[] options) {    if (options == null)      return -1;        for (int i = 0; i < options.length; i++) {      if ((options[i].length() > 0) && (options[i].charAt(0) == '-')) {	// Check if it is a negative number	try {	  Double.valueOf(options[i]);	} 	catch (NumberFormatException e) {	  // found?	  if (options[i].equals("-" + flag))	    return i;	  // did we reach "--"?	  if (options[i].charAt(1) == '-')	    return -1;	}      }    }        return -1;  }  /**   * Quotes a string if it contains special characters.   *    * The following rules are applied:   *   * A character is backquoted version of it is one    * of <tt>" ' % \ \n \r \t</tt>.   *   * A string is enclosed within single quotes if a character has been   * backquoted using the previous rule above or contains    * <tt>{ }</tt> or is exactly equal to the strings    * <tt>, ? space or ""</tt> (empty string).   *   * A quoted question mark distinguishes it from the missing value which   * is represented as an unquoted question mark in arff files.   *   * @param string 	the string to be quoted   * @return 		the string (possibly quoted)   * @see		#unquote(String)   */  public static /*@pure@*/ String quote(String string) {      boolean quote = false;      // backquote the following characters       if ((string.indexOf('\n') != -1) || (string.indexOf('\r') != -1) || 	  (string.indexOf('\'') != -1) || (string.indexOf('"') != -1) || 	  (string.indexOf('\\') != -1) || 	  (string.indexOf('\t') != -1) || (string.indexOf('%') != -1)) {	  string = backQuoteChars(string);	  quote = true;      }      // Enclose the string in 's if the string contains a recently added      // backquote or contains one of the following characters.      if((quote == true) || 	 (string.indexOf('{') != -1) || (string.indexOf('}') != -1) ||	 (string.indexOf(',') != -1) || (string.equals("?")) ||	 (string.indexOf(' ') != -1) || (string.equals(""))) {	  string = ("'".concat(string)).concat("'");      }      return string;  }  /**   * unquotes are previously quoted string (but only if necessary), i.e., it   * removes the single quotes around it. Inverse to quote(String).   *    * @param string	the string to process   * @return		the unquoted string   * @see		#quote(String)   */  public static String unquote(String string) {    if (string.startsWith("'") && string.endsWith("'")) {      string = string.substring(1, string.length() - 1);            if ((string.indexOf("\\n") != -1) || (string.indexOf("\\r") != -1) || 	  (string.indexOf("\\'") != -1) || (string.indexOf("\\\"") != -1) || 	  (string.indexOf("\\\\") != -1) || 	  (string.indexOf("\\t") != -1) || (string.indexOf("\\%") != -1)) {	string = unbackQuoteChars(string);      }    }    return string;  }  /**   * Converts carriage returns and new lines in a string into \r and \n.   * Backquotes the following characters: ` " \ \t and %   *    * @param string 	the string   * @return 		the converted string   * @see		#unbackQuoteChars(String)   */  public static /*@pure@*/ String backQuoteChars(String string) {    int index;    StringBuffer newStringBuffer;    // replace each of the following characters with the backquoted version    char   charsFind[] =    {'\\',   '\'',  '\t',  '\n',  '\r',  '"',    '%'};    String charsReplace[] = {"\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"", "\\%"};    for (int i = 0; i < charsFind.length; i++) {      if (string.indexOf(charsFind[i]) != -1 ) {	newStringBuffer = new StringBuffer();	while ((index = string.indexOf(charsFind[i])) != -1) {	  if (index > 0) {	    newStringBuffer.append(string.substring(0, index));	  }	  newStringBuffer.append(charsReplace[i]);	  if ((index + 1) < string.length()) {	    string = string.substring(index + 1);	  } else {	    string = "";	  }	}	newStringBuffer.append(string);	string = newStringBuffer.toString();      }    }    return string;  }  /**   * Converts carriage returns and new lines in a string into \r and \n.   *   * @param string the string   * @return the converted string   */  public static String convertNewLines(String string) {    int index;    // Replace with \n    StringBuffer newStringBuffer = new StringBuffer();    while ((index = string.indexOf('\n')) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\\');      newStringBuffer.append('n');      if ((index + 1) < string.length()) {	string = string.substring(index + 1);      } else {	string = "";      }    }    newStringBuffer.append(string);    string = newStringBuffer.toString();    // Replace with \r    newStringBuffer = new StringBuffer();    while ((index = string.indexOf('\r')) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\\');      newStringBuffer.append('r');      if ((index + 1) < string.length()){	string = string.substring(index + 1);      } else {	string = "";      }    }    newStringBuffer.append(string);    return newStringBuffer.toString();  }  /**   * Reverts \r and \n in a string into carriage returns and new lines.   *    * @param string the string   * @return the converted string   */  public static String revertNewLines(String string) {    int index;    // Replace with \n    StringBuffer newStringBuffer = new StringBuffer();    while ((index = string.indexOf("\\n")) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\n');      if ((index + 2) < string.length()) {	string = string.substring(index + 2);      } else {	string = "";      }    }    newStringBuffer.append(string);    string = newStringBuffer.toString();    // Replace with \r    newStringBuffer = new StringBuffer();    while ((index = string.indexOf("\\r")) != -1) {      if (index > 0) {	newStringBuffer.append(string.substring(0, index));      }      newStringBuffer.append('\r');      if ((index + 2) < string.length()){	string = string.substring(index + 2);      } else {	string = "";      }    }    newStringBuffer.append(string);        return newStringBuffer.toString();  }  /**   * Returns the secondary set of options (if any) contained in   * the supplied options array. The secondary set is defined to   * be any options after the first "--". These options are removed from   * the original options array.   *   * @param options the input array of options   * @return the array of secondary options   */  public static String[] partitionOptions(String[] options) {    for (int i = 0; i < options.length; i++) {      if (options[i].equals("--")) {	options[i++] = "";	String[] result = new String [options.length - i];	for (int j = i; j < options.length; j++) {	  result[j - i] = options[j];	  options[j] = "";	}	return result;      }    }    return new String [0];  }      /**   * The inverse operation of backQuoteChars().   * Converts back-quoted carriage returns and new lines in a string    * to the corresponding character ('\r' and '\n').   * Also "un"-back-quotes the following characters: ` " \ \t and %   *   * @param string 	the string   * @return 		the converted string   * @see		#backQuoteChars(String)   */  public static String unbackQuoteChars(String string) {    int index;    StringBuffer newStringBuffer;        // replace each of the following characters with the backquoted version    String charsFind[]    = {"\\\\", "\\'", "\\t", "\\n", "\\r", "\\\"", "\\%"};    char   charsReplace[] = {'\\',   '\'',  '\t',  '\n',  '\r',  '"',    '%'};    int pos[] = new int[charsFind.length];    int	curPos;        String str = new String(string);    newStringBuffer = new StringBuffer();    while (str.length() > 0) {      // get positions and closest character to replace      curPos = str.length();      index  = -1;      for (int i = 0; i < pos.length; i++) {	pos[i] = str.indexOf(charsFind[i]);	if ( (pos[i] > -1) && (pos[i] < curPos) ) {	  index  = i;	  curPos = pos[i];	}      }            // replace character if found, otherwise finished      if (index == -1) {	newStringBuffer.append(str);	str = "";      }      else {	newStringBuffer.append(str.substring(0, pos[index]));	newStringBuffer.append(charsReplace[index]);	str = str.substring(pos[index] + charsFind[index].length());      }    }    return newStringBuffer.toString();  }        /**   * Split up a string containing options into an array of strings,   * one for each option.   *   * @param 		quotedOptionString the string containing the options   * @return 		the array of options   * @throws Exception 	in case of an unterminated string, unknown character or   * 			a parse error   */  public static String[] splitOptions(String quotedOptionString) throws Exception{    FastVector optionsVec = new FastVector();    String str = new String(quotedOptionString);    int i;        while (true){      //trimLeft       i = 0;      while ((i < str.length()) && (Character.isWhitespace(str.charAt(i)))) i++;      str = str.substring(i);            //stop when str is empty      if (str.length() == 0) break;            //if str start with a double quote      if (str.charAt(0) == '"'){		//find the first not anti-slached double quote	i = 1;	while(i < str.length()){	  if (str.charAt(i) == str.charAt(0)) break;	  if (str.charAt(i) == '\\'){	    i += 1;	    if (i >= str.length()) 	      throw new Exception("String should not finish with \\");	  }	  i += 1;	}	if (i >= str.length()) throw new Exception("Quote parse error.");		//add the founded string to the option vector (without quotes)	String optStr = str.substring(1,i);	optStr = unbackQuoteChars(optStr);	optionsVec.addElement(optStr);	str = str.substring(i+1);      } else {	//find first whiteSpace	i=0;	while((i < str.length()) && (!Character.isWhitespace(str.charAt(i)))) i++;		//add the founded string to the option vector	String optStr = str.substring(0,i);	optionsVec.addElement(optStr);	str = str.substring(i);      }    }        //convert optionsVec to an array of String    String[] options = new String[optionsVec.size()];    for (i = 0; i < optionsVec.size(); i++) {      options[i] = (String)optionsVec.elementAt(i);    }    return options;  }      /**   * Joins all the options in an option array into a single string,   * as might be used on the command line.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -