1
/*------------------------------------------------------------------------------
2
* Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
4
* Distributable under the terms of either the Apache License (Version 2.0) or
5
* the GNU Lesser General Public License, as specified in the COPYING file.
6
------------------------------------------------------------------------------*/
7
#include "CLucene/_ApiHeader.h"
9
#include "NumberTools.h"
10
#include "CLucene/util/Misc.h"
14
const TCHAR* NumberTools::MIN_STRING_VALUE = NEGATIVE_PREFIX _T("0000000000000");
15
const TCHAR* NumberTools::MAX_STRING_VALUE = POSITIVE_PREFIX _T("1y2p0ij32e8e7");
17
TCHAR* NumberTools::longToString(int64_t l)
19
if (l == LUCENE_INT64_MIN_SHOULDBE) {
20
// special case, because long is not symetric around zero
21
return stringDuplicate(MIN_STRING_VALUE);
24
TCHAR* buf = _CL_NEWARRAY(TCHAR, STR_SIZE + 1);
26
buf[0] = NEGATIVE_PREFIX[0];
27
l = LUCENE_INT64_MAX_SHOULDBE + l + 1;
29
buf[0] = POSITIVE_PREFIX[0];
33
_i64tot(l, tmp, NUMBERTOOLS_RADIX);
34
size_t len = _tcslen(tmp);
35
_tcscpy(buf+(STR_SIZE-len),tmp);
36
for ( size_t i=1;i<STR_SIZE-len;i++ )
44
int64_t NumberTools::stringToLong(const TCHAR* str) {
46
_CLTHROWA(CL_ERR_NullPointer,"string cannot be null");
48
if (_tcslen(str) != STR_SIZE) {
49
_CLTHROWA(CL_ERR_NumberFormat,"string is the wrong size");
52
if (_tcscmp(str, MIN_STRING_VALUE) == 0) {
53
return LUCENE_INT64_MIN_SHOULDBE;
56
TCHAR prefix = str[0];
58
TCHAR* sentinel = NULL;
59
int64_t l = _tcstoi64(++str, &sentinel, NUMBERTOOLS_RADIX);
61
if (prefix == POSITIVE_PREFIX[0]) {
63
} else if (prefix == NEGATIVE_PREFIX[0]) {
64
l = l - LUCENE_INT64_MAX_SHOULDBE - 1;
66
_CLTHROWA(CL_ERR_NumberFormat,"string does not begin with the correct prefix");
72
NumberTools::~NumberTools(){