mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-14 17:24:01 +00:00
ICU-1533 per Doug's review, move appendNumber to a utility class
X-SVN-Rev: 6999
This commit is contained in:
parent
f9dc66097b
commit
a807740f12
4 changed files with 90 additions and 22 deletions
|
@ -71,7 +71,7 @@ cpdtrans.o hextouni.o rbt.o rbt_data.o rbt_pars.o rbt_rule.o rbt_set.o \
|
|||
dbbi.o dbbi_tbl.o rbbi.o rbbi_tbl.o nultrans.o \
|
||||
remtrans.o titletrn.o tolowtrn.o toupptrn.o xformtrn.o \
|
||||
name2uni.o uni2name.o unitohex.o nortrans.o unifilt.o quant.o transreg.o \
|
||||
llong.o nfrs.o nfrule.o nfsubs.o rbnf.o upropset.o
|
||||
llong.o nfrs.o nfrule.o nfsubs.o rbnf.o upropset.o util.o
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include "quant.h"
|
||||
#include "unicode/unistr.h"
|
||||
#include "util.h"
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
|
@ -71,25 +72,6 @@ UMatchDegree Quantifier::matches(const Replaceable& text,
|
|||
return U_MISMATCH;
|
||||
}
|
||||
|
||||
static const int32_t POW10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
|
||||
10000000, 100000000, 1000000000};
|
||||
|
||||
void Quantifier::appendNumber(UnicodeString& result, int32_t n) {
|
||||
// assert(n >= 0);
|
||||
// assert(n < 1e10);
|
||||
UBool show = FALSE; // TRUE if we should display digits
|
||||
for (int32_t p=9; p>=0; --p) {
|
||||
int32_t d = n / POW10[p];
|
||||
n -= d * POW10[p];
|
||||
if (d != 0 || p == 0) {
|
||||
show = TRUE;
|
||||
}
|
||||
if (show) {
|
||||
result.append((UChar)(48+d));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement UnicodeMatcher
|
||||
*/
|
||||
|
@ -107,10 +89,10 @@ UnicodeString& Quantifier::toPattern(UnicodeString& result,
|
|||
return result.append((UChar)43); /*+*/
|
||||
}
|
||||
result.append((UChar)123); /*{*/
|
||||
appendNumber(result, minCount);
|
||||
Utility::appendNumber(result, minCount);
|
||||
result.append((UChar)44); /*,*/
|
||||
if (maxCount != MAX) {
|
||||
appendNumber(result, maxCount);
|
||||
Utility::appendNumber(result, maxCount);
|
||||
}
|
||||
result.append((UChar)125); /*}*/
|
||||
return result;
|
||||
|
|
49
icu4c/source/i18n/util.cpp
Normal file
49
icu4c/source/i18n/util.cpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2001, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* Date Name Description
|
||||
* 11/19/2001 aliu Creation.
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
#include "util.h"
|
||||
|
||||
// "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
static const UChar DIGITS[] = {
|
||||
48,49,50,51,52,53,54,55,56,57,
|
||||
65,66,67,68,69,70,71,72,73,74,
|
||||
75,76,77,78,79,80,81,82,83,84,
|
||||
85,86,87,88,89,90
|
||||
};
|
||||
|
||||
UnicodeString& Utility::appendNumber(UnicodeString& result, int32_t n,
|
||||
int32_t radix) {
|
||||
if (radix < 2 || radix > 36) {
|
||||
// Bogus radix
|
||||
return result.append((UChar)63/*?*/);
|
||||
}
|
||||
// Handle negatives
|
||||
if (n < 0) {
|
||||
n = -n;
|
||||
result.append((UChar)45/*-*/);
|
||||
}
|
||||
// First determine the number of digits
|
||||
int32_t nn = n;
|
||||
int32_t r = 1;
|
||||
while (nn >= radix) {
|
||||
nn /= radix;
|
||||
r *= radix;
|
||||
}
|
||||
// Now generate the digits
|
||||
while (r > 0) {
|
||||
int32_t digit = n / r;
|
||||
result.append(DIGITS[digit]);
|
||||
n -= digit * r;
|
||||
r /= radix;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//eof
|
37
icu4c/source/i18n/util.h
Normal file
37
icu4c/source/i18n/util.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
**********************************************************************
|
||||
* Copyright (c) 2001, International Business Machines
|
||||
* Corporation and others. All Rights Reserved.
|
||||
**********************************************************************
|
||||
* Date Name Description
|
||||
* 11/19/2001 aliu Creation.
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
#include "unicode/utypes.h"
|
||||
#include "unicode/unistr.h"
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// class Utility
|
||||
// i18n utility functions, scoped into the class Utility.
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
U_NAMESPACE_BEGIN
|
||||
|
||||
class Utility {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Append a number to the given UnicodeString in the given radix.
|
||||
* The radix must be between 2 and 36, inclusive. Standard digits
|
||||
* '0'-'9' are used and letters 'A'-'Z' for radices 11 through 36.
|
||||
* If n is negative, a '-' is prepended.
|
||||
*/
|
||||
static UnicodeString& appendNumber(UnicodeString& result, int32_t n,
|
||||
int32_t radix = 10);
|
||||
|
||||
};
|
||||
|
||||
U_NAMESPACE_END
|
||||
|
||||
//eof
|
Loading…
Add table
Reference in a new issue