PageRenderTime 70ms CodeModel.GetById 59ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/u64.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 105 lines | 64 code | 15 blank | 26 comment | 6 complexity | 9a0a2884206b0eb3776c70679d6c6777 MD5 | raw file
  1/** 
  2 * @file u64.cpp
  3 * @brief Utilities to deal with U64s.
  4 *
  5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "u64.h"
 30
 31
 32U64 str_to_U64(const std::string& str)
 33{
 34	U64 result = 0;
 35	const char *aptr = strpbrk(str.c_str(),"0123456789");
 36
 37	if (!aptr)
 38	{
 39		llwarns << "str_to_U64: Bad string to U64 conversion attempt: format\n" << llendl;
 40	}
 41	else
 42	{
 43		while ((*aptr >= '0') && (*aptr <= '9'))
 44		{
 45			result = result*10 + (*aptr++ - '0');
 46		}
 47	}
 48	return (result);
 49}
 50
 51
 52std::string U64_to_str(U64 value) 
 53{
 54	std::string res;
 55	U32 part1,part2,part3;
 56	
 57	part3 = (U32)(value % (U64)10000000);
 58	
 59	value /= 10000000;
 60	part2 = (U32)(value % (U64)10000000);
 61	
 62	value /= 10000000;
 63	part1 = (U32)(value % (U64)10000000);
 64	
 65	// three cases to avoid leading zeroes unless necessary
 66	
 67	if (part1)
 68	{
 69		res = llformat("%u%07u%07u",part1,part2,part3);
 70	}
 71	else if (part2)
 72	{
 73		res = llformat("%u%07u",part2,part3);
 74	}
 75	else
 76	{
 77		res = llformat("%u",part3);	
 78	}
 79	return res;
 80} 
 81
 82char* U64_to_str(U64 value, char* result, S32 result_size) 
 83{
 84	std::string res = U64_to_str(value);
 85	LLStringUtil::copy(result, res.c_str(), result_size);
 86	return result;
 87}
 88
 89F64 U64_to_F64(const U64 value)
 90{
 91	S64 top_bits = (S64)(value >> 1);
 92	F64 result = (F64)top_bits;
 93	result *= 2.f;
 94	result += (U32)(value & 0x01);
 95	return result;
 96}
 97
 98U64	llstrtou64(const char* str, char** end, S32 base)
 99{
100#ifdef LL_WINDOWS
101	return _strtoui64(str,end,base);
102#else
103	return strtoull(str,end,base);
104#endif
105}