📄 formattable.cpp
字号:
#include <lang/Formattable.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <assert.h>
#include <config.h>
namespace lang
{
static void parseNumberPattern(
const String& pattern, int pos,
int& minIntDigits, int& maxIntDigits,
int& minFracDigits, int& maxFracDigits,
bool& fraction, bool& grouping, char& hex )
{
const int absMaxIntDigits = 20;
minIntDigits = 0;
maxIntDigits = 0;
minFracDigits = 0;
maxFracDigits = 0;
fraction = false;
grouping = false;
hex = 0;
int patternEnd = pos;
for ( ; patternEnd < pattern.length() ; ++patternEnd )
if ( pattern.charAt(patternEnd) == char('}') )
break;
for ( int i = pos ; i < patternEnd ; ++i )
{
char ch = pattern.charAt(i);
if ( ch == 'x' || ch == 'X' )
{
hex = (char)ch;
break;
}
else if ( ch == char('.') )
{
fraction = true;
}
else if ( ch == char(',') )
{
grouping = true;
}
else if ( ch == char('0') )
{
// compulsory digit
if ( fraction )
++minFracDigits, ++maxFracDigits;
else
++minIntDigits, maxIntDigits=absMaxIntDigits;
}
else if ( ch == char('#') )
{
// optional digit
if ( fraction )
++maxFracDigits;
else
maxIntDigits=absMaxIntDigits;
}
}
}
Formattable::Formattable()
{
m_type = VALUE_NONE;
}
Formattable::Formattable( int value )
{
m_type = VALUE_DOUBLE;
m_dbl = value;
}
Formattable::Formattable( const String& value )
{
m_type = VALUE_STRING;
m_str = value;
}
Formattable::Formattable( const char* value )
{
m_type = VALUE_STRING;
m_str = value;
}
Formattable::ValueType Formattable::type() const
{
return m_type;
}
int Formattable::doubleValue() const
{
assert( m_type == VALUE_DOUBLE );
return m_dbl;
}
String Formattable::stringValue() const
{
assert( m_type == VALUE_STRING );
return m_str;
}
int Formattable::format( char* buffer, int size, const String& pattern, int pos ) const
{
if ( VALUE_DOUBLE == m_type )
{
int minIntDigits, maxIntDigits;
int minFracDigits, maxFracDigits;
bool fraction, grouping;
char hex;
parseNumberPattern( pattern, pos, minIntDigits, maxIntDigits, minFracDigits, maxFracDigits, fraction, grouping, hex );
bool chopInt = false;
char fmt[32];
if ( hex )
sprintf( fmt, "%%%c", hex ), chopInt = true;
else
sprintf( fmt, "%%d" ), chopInt = true;
char buff[32];
sprintf( buff, fmt, (int)m_dbl );
int needed = strlen(buff);
int count = needed;
if ( count > size )
count = size;
for ( int i = 0 ; i < count ; ++i )
buffer[i] = buff[i];
if ( needed < size )
buffer[needed] = 0;
else if ( size > 0 )
buffer[size-1] = 0;
return needed;
}
else if ( VALUE_STRING == m_type )
{
int needed = m_str.length();
int count = needed;
if ( count > size )
count = size;
m_str.getChars( 0, count, buffer );
if ( needed < size )
buffer[needed] = 0;
else if ( size > 0 )
buffer[size-1] = 0;
return needed;
}
return 0;
}
} // lang
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -