From 037ea84ba59bd1af39ed86f807827d6c4466da1a Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sun, 23 Jul 2017 11:40:25 +0200 Subject: [PATCH] BNumberFormat: add Parse() and GetSymbol(). --- headers/os/locale/NumberFormat.h | 10 +++--- src/kits/locale/NumberFormat.cpp | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/headers/os/locale/NumberFormat.h b/headers/os/locale/NumberFormat.h index 0a9b4e0a0c..834632ac9f 100644 --- a/headers/os/locale/NumberFormat.h +++ b/headers/os/locale/NumberFormat.h @@ -10,10 +10,8 @@ enum BNumberElement { - B_NUMBER_ELEMENT_INVALID = B_BAD_DATA, - B_NUMBER_ELEMENT_INTEGER = 0, - B_NUMBER_ELEMENT_FRACTIONAL, - B_NUMBER_ELEMENT_CURRENCY + B_DECIMAL_SEPARATOR = 10, // Values 0-9 are reserved for digit symbols + B_GROUPING_SEPARATOR, }; class BNumberFormatImpl; @@ -36,6 +34,10 @@ public: status_t FormatMonetary(BString& string, const double value); + status_t Parse(const BString& string, double& value); + + BString GetSeparator(BNumberElement element); + private: BNumberFormat(const BNumberFormat &other); diff --git a/src/kits/locale/NumberFormat.cpp b/src/kits/locale/NumberFormat.cpp index d2fed1a1ef..0b95a73051 100644 --- a/src/kits/locale/NumberFormat.cpp +++ b/src/kits/locale/NumberFormat.cpp @@ -2,6 +2,7 @@ * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de. * Copyright 2012, John Scipione, jscipione@gmail.com + * Copyright 2017, Adrien Destugues, pulkomandy@pulkomandy.tk * All rights reserved. Distributed under the terms of the MIT License. */ @@ -15,6 +16,8 @@ #include +#include +#include #include @@ -231,3 +234,60 @@ BNumberFormat::FormatMonetary(BString& string, const double value) return B_OK; } + + +status_t +BNumberFormat::Parse(const BString& string, double& value) +{ + NumberFormat* parser = fPrivateData->GetFloat(&fConventions); + + if (parser == NULL) + return B_NO_MEMORY; + + UnicodeString unicode(string.String()); + Formattable result(0); + UErrorCode err = U_ZERO_ERROR; + + parser->parse(unicode, result, err); + + if (err != U_ZERO_ERROR) + return B_BAD_DATA; + + value = result.getDouble(); + + return B_OK; +} + + +BString +BNumberFormat::GetSeparator(BNumberElement element) +{ + DecimalFormatSymbols::ENumberFormatSymbol symbol; + BString result; + + switch(element) { + case B_DECIMAL_SEPARATOR: + symbol = DecimalFormatSymbols::kDecimalSeparatorSymbol; + break; + case B_GROUPING_SEPARATOR: + symbol = DecimalFormatSymbols::kGroupingSeparatorSymbol; + break; + + default: + return result; + } + + NumberFormat* format = fPrivateData->GetFloat(&fConventions); + DecimalFormat* decimal = dynamic_cast(format); + + if (decimal == NULL) + return result; + + const DecimalFormatSymbols* symbols = decimal->getDecimalFormatSymbols(); + UnicodeString string = symbols->getSymbol(symbol); + + BStringByteSink stringConverter(&result); + string.toUTF8(stringConverter); + + return result; +}