Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2215)

Unified Diff: main/classes/core/src/newapi/impl/PaddingImpl.java

Issue 329960043: ticket:13177 NumberFormat2 from Shane's branch Base URL: svn+ssh://source.icu-project.org/repos/icu/trunk/icu4j/
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: main/classes/core/src/newapi/impl/PaddingImpl.java
===================================================================
--- main/classes/core/src/newapi/impl/PaddingImpl.java (revision 0)
+++ main/classes/core/src/newapi/impl/PaddingImpl.java (working copy)
@@ -0,0 +1,109 @@
+// © 2017 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+package newapi.impl;
+
+import com.ibm.icu.impl.number.NumberStringBuilder;
+import com.ibm.icu.impl.number.formatters.PaddingFormat.PadPosition;
+
+import newapi.NumberFormatter.Padding;
+
+public class PaddingImpl extends Padding.Internal {
+
+ String paddingString;
+ int targetWidth;
+ PadPosition position;
+
+ public static final PaddingImpl NONE = new PaddingImpl();
+
+ public static PaddingImpl getInstance(
+ String paddingString, int targetWidth, PadPosition position) {
+ // TODO: Add a few default implementations
+ return new PaddingImpl(paddingString, targetWidth, position);
+ }
+
+ /** Default constructor, producing an empty instance */
+ public PaddingImpl() {
+ paddingString = null;
+ targetWidth = -1;
+ position = null;
+ }
+
+ private PaddingImpl(String paddingString, int targetWidth, PadPosition position) {
+ this.paddingString = (paddingString == null) ? " " : paddingString;
+ this.targetWidth = targetWidth;
+ this.position = (position == null) ? PadPosition.BEFORE_PREFIX : position;
+ }
+
+ public int applyModsAndMaybePad(
+ MicroProps micros, NumberStringBuilder string, int leftIndex, int rightIndex) {
+ // Apply modInner (scientific notation) before padding
+ int innerLength = micros.modInner.apply(string, leftIndex, rightIndex);
+
+ // No padding; apply the mods and leave.
+ if (targetWidth < 0) {
+ return applyMicroMods(micros, string, leftIndex, rightIndex + innerLength);
+ }
+
+ // Estimate the padding width needed.
+ // TODO: Make this more efficient (less copying)
+ // TODO: How to handle when padding is inserted between a currency sign and the number
+ // when currency spacing is in play?
+ NumberStringBuilder backup = new NumberStringBuilder(string);
+ int length = innerLength + applyMicroMods(micros, string, leftIndex, rightIndex + innerLength);
+ int requiredPadding = targetWidth - string.codePointCount();
+
+ if (requiredPadding <= 0) {
+ // Padding is not required.
+ return length;
+ }
+
+ length = innerLength;
+ string.copyFrom(backup);
+ if (position == PadPosition.AFTER_PREFIX) {
+ length += addPaddingHelper(paddingString, requiredPadding, string, leftIndex);
+ } else if (position == PadPosition.BEFORE_SUFFIX) {
+ length += addPaddingHelper(paddingString, requiredPadding, string, rightIndex + length);
+ }
+ length += applyMicroMods(micros, string, leftIndex, rightIndex + length);
+ if (position == PadPosition.BEFORE_PREFIX) {
+ length = addPaddingHelper(paddingString, requiredPadding, string, leftIndex);
+ } else if (position == PadPosition.AFTER_SUFFIX) {
+ length = addPaddingHelper(paddingString, requiredPadding, string, rightIndex + length);
+ }
+
+ // The length might not be exactly right due to currency spacing.
+ // Make an adjustment if needed.
+ while (string.codePointCount() < targetWidth) {
+ int insertIndex;
+ switch (position) {
+ case AFTER_PREFIX:
+ insertIndex = leftIndex + length;
+ break;
+ case BEFORE_SUFFIX:
+ insertIndex = rightIndex + length;
+ break;
+ default:
+ // Should not happen since currency spacing is always on the inside.
+ throw new AssertionError();
+ }
+ length += string.insert(insertIndex, paddingString, null);
+ }
+
+ return length;
+ }
+
+ private static int applyMicroMods(
+ MicroProps micros, NumberStringBuilder string, int leftIndex, int rightIndex) {
+ int length = micros.modMiddle.apply(string, leftIndex, rightIndex);
+ length += micros.modOuter.apply(string, leftIndex, rightIndex + length);
+ return length;
+ }
+
+ private static int addPaddingHelper(
+ String paddingString, int requiredPadding, NumberStringBuilder string, int index) {
+ for (int i = 0; i < requiredPadding; i++) {
+ string.insert(index, paddingString, null);
+ }
+ return paddingString.length() * requiredPadding;
+ }
+}
Property changes on: main/classes/core/src/newapi/impl/PaddingImpl.java
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain;charset=utf-8
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b