insert() private method

private insert ( int par0, bool par1 ) : global::java.lang.StringBuilder
par0 int
par1 bool
return global::java.lang.StringBuilder
Ejemplo n.º 1
0
 // Some national prefixes are a substring of others. If extracting the shorter NDD doesn't result
 // in a number we can format, we try to see if we can extract a longer version here.
 private boolean ableToExtractLongerNdd()
 {
     if (nationalPrefixExtracted.length() > 0)
     {
         // Put the extracted NDD back to the national number before attempting to extract a new NDD.
         nationalNumber.insert(0, nationalPrefixExtracted);
         // Remove the previously extracted NDD from prefixBeforeNationalNumber. We cannot simply set
         // it to empty string because people sometimes incorrectly enter national prefix after the
         // country code, e.g. +44 (0)20-1234-5678.
         int indexOfPreviousNdd = prefixBeforeNationalNumber.lastIndexOf(nationalPrefixExtracted);
         prefixBeforeNationalNumber.setLength(indexOfPreviousNdd);
     }
     return(!nationalPrefixExtracted.equals(removeNationalPrefixFromNationalNumber()));
 }
Ejemplo n.º 2
0
		private void transformA (StringBuilder result)
		{
			if ((arg is float) || (arg is double)) {
				result.append (String.Format ("{0:x}", arg));
			} else {
				throw badArgumentType ();
			}
			if (!formatToken.isPrecisionSet ()) {
				return;
			}
			int precision = formatToken.getPrecision ();
			if (precision == 0) {
				precision = 1;
			}
			int indexOfFirstFractionalDigit = result.indexOf (".") + 1;
			int indexOfP = result.indexOf ("p");
			int fractionalLength = indexOfP - indexOfFirstFractionalDigit;
			if (fractionalLength == precision) {
				return;
			}
			if (fractionalLength < precision) {
				char[] zeros = new char[precision - fractionalLength];
				java.util.Arrays.fill (zeros, '0');
				// %a shouldn't be localized.
				result.insert (indexOfP, zeros);
				return;
			}
			result.delete (indexOfFirstFractionalDigit + precision, indexOfP);
		}
Ejemplo n.º 3
0
		private CharSequence transformFromFloat ()
		{
			if (arg == null) {
				return transformFromNull ();
			} else {
				if (arg is float) {
					float f = (float)arg;
					if (f != f || f == float.PositiveInfinity || f == float.NegativeInfinity)
						return transformFromSpecialNumber ((double)f);
				} else if (arg is double) {
					double d = (double)arg;
					if (d != d || d == double.PositiveInfinity || d == double.NegativeInfinity)
						return transformFromSpecialNumber (d);
				} else {
					if (arg is decimal) {
					} else {
						// BigDecimal can't represent NaN or infinities, but its doubleValue method will return
						// infinities if the BigDecimal is too big for a double.
						throw badArgumentType ();
					}
				}
			}
			char conversionType = formatToken.getConversionType ();
			if (conversionType != 'a' && conversionType != 'A' && !formatToken.isPrecisionSet
				()) {
				formatToken.setPrecision (java.util.Formatter.FormatToken.DEFAULT_PRECISION);
			}
			StringBuilder result = new StringBuilder ();
			switch (conversionType) {
			case 'a':
			case 'A':
				{
					transformA (result);
					break;
				}

			case 'e':
			case 'E':
				{
					transformE (result);
					break;
				}

			case 'f':
				{
					transformF (result);
					break;
				}

			case 'g':
			case 'G':
				{
					transformG (result);
					break;
				}

			default:
				{
					throw formatToken.unknownFormatConversionException ();
				}
			}
			formatToken.setPrecision (java.util.Formatter.FormatToken.UNSET);
			int startIndex = 0;
			if (result [0] == localeData.minusSign) {
				if (formatToken.flagParenthesis) {
					return wrapParentheses (result);
				}
			} else {
				if (formatToken.flagSpace) {
					result.insert (0, ' ');
					startIndex++;
				}
				if (formatToken.flagPlus) {
					result.insert (0, '+');
					startIndex++;
				}
			}
			char firstChar = result [0];
			if (formatToken.flagZero && (firstChar == '+' || firstChar == localeData.minusSign
				)) {
				startIndex = 1;
			}
			if (conversionType == 'a' || conversionType == 'A') {
				startIndex += 2;
			}
			return padding (result, startIndex);
		}
Ejemplo n.º 4
0
		// BigDecimal can't represent NaN or infinities, but its doubleValue method will return
		// infinities if the BigDecimal is too big for a double.
		private void transformE (StringBuilder result)
		{
			// All zeros in this method are *pattern* characters, so no localization.
			int precision = formatToken.getPrecision ();
			string pattern = "0E+00";
			if (precision > 0) {
				StringBuilder sb = new StringBuilder ("0.");
				char[] zeros = new char[precision];
				java.util.Arrays.fill (zeros, '0');
				sb.append (zeros);
				sb.append ("E+00");
				pattern = sb.ToString ();
			}
			result.append (arg.ToString ().Replace ('E', 'e'));
			// The # flag requires that we always output a decimal separator.
			if (formatToken.flagSharp && precision == 0) {
				int indexOfE = result.indexOf ("e");
				result.insert (indexOfE, localeData.decimalSeparator);
			}
		}
Ejemplo n.º 5
0
		private CharSequence transformFromInteger ()
		{
			int startIndex = 0;
			StringBuilder result = new StringBuilder ();
			char currentConversionType = formatToken.getConversionType ();
			long value;
			if (arg is long) {
				value = ((long)arg);
			} else {
				if (arg is int) {
					value = (int)arg;
				} else {
					if (arg is short) {
						value = (short)arg;
					} else {
						if (arg is byte) {
							value = (byte)arg;
						} else {
							throw badArgumentType ();
						}
					}
				}
			}
			if (formatToken.flagSharp) {
				if (currentConversionType == 'o') {
					result.append ("0");
					startIndex += 1;
				} else {
					result.append ("0x");
					startIndex += 2;
				}
			}
			if (currentConversionType == 'd') {
				CharSequence digits = CharSequenceProxy.Wrap (System.Convert.ToString
					(value));
				if (formatToken.flagComma) {
					digits = insertGrouping (digits);
				}
				if (localeData.zeroDigit != '0') {
					digits = localizeDigits (digits);
				}
				result.append (digits);
				if (value < 0) {
					if (formatToken.flagParenthesis) {
						return wrapParentheses (result);
					} else {
						if (formatToken.flagZero) {
							startIndex++;
						}
					}
				} else {
					if (formatToken.flagPlus) {
						result.insert (0, '+');
						startIndex += 1;
					} else {
						if (formatToken.flagSpace) {
							result.insert (0, ' ');
							startIndex += 1;
						}
					}
				}
			} else {
				// Undo sign-extension, since we'll be using Long.to(Octal|Hex)String.
				if (arg is byte) {
					value &= unchecked((long)(0xffL));
				} else {
					if (arg is short) {
						value &= unchecked((long)(0xffffL));
					} else {
						if (arg is int) {
							value &= unchecked((long)(0xffffffffL));
						}
					}
				}
				if (currentConversionType == 'o') {
					result.append (Convert.ToString (value, 10));
				} else {
					result.append (Convert.ToString (value, 16));
				}
			}
			return padding (result, startIndex);
		}