0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

move hex::val to util/hex.h and rename to fromHex

This commit is contained in:
Mathias Stearn 2010-03-03 21:39:14 -05:00
parent e259b94f55
commit 59de6c4b38
2 changed files with 40 additions and 20 deletions

View File

@ -20,6 +20,7 @@
#include "json.h"
#include "../util/builder.h"
#include "../util/base64.h"
#include "../util/hex.h"
using namespace boost::spirit;
@ -167,27 +168,11 @@ namespace mongo {
ObjectBuilder &b;
};
namespace hex {
int val( char c ) {
if ( '0' <= c && c <= '9' )
return c - '0';
if ( 'a' <= c && c <= 'f' )
return c - 'a' + 10;
if ( 'A' <= c && c <= 'F' )
return c - 'A' + 10;
assert( false );
return 0xff;
}
char val( const char *c ) {
return ( val( c[ 0 ] ) << 4 ) | val( c[ 1 ] );
}
} // namespace hex
struct chU {
chU( ObjectBuilder &_b ) : b( _b ) {}
void operator() ( const char *start, const char *end ) const {
unsigned char first = hex::val( start );
unsigned char second = hex::val( start + 2 );
unsigned char first = fromHex( start );
unsigned char second = fromHex( start + 2 );
if ( first == 0 && second < 0x80 )
b.ss << second;
else if ( first < 0x08 ) {
@ -315,7 +300,7 @@ namespace mongo {
OID oid;
char *oidP = (char *)( &oid );
for ( int i = 0; i < 12; ++i )
oidP[ i ] = hex::val( s + ( i * 2 ) );
oidP[ i ] = fromHex( s + ( i * 2 ) );
return oid;
}
@ -356,7 +341,7 @@ namespace mongo {
struct binDataType {
binDataType( ObjectBuilder &_b ) : b( _b ) {}
void operator() ( const char *start, const char *end ) const {
b.binDataType = BinDataType( hex::val( start ) );
b.binDataType = BinDataType( fromHex( start ) );
}
ObjectBuilder &b;
};

35
util/hex.h Normal file
View File

@ -0,0 +1,35 @@
// util/hex.h
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
namespace mongo {
//can't use hex namespace because it conflicts with hex iostream function
inline int fromHex( char c ) {
if ( '0' <= c && c <= '9' )
return c - '0';
if ( 'a' <= c && c <= 'f' )
return c - 'a' + 10;
if ( 'A' <= c && c <= 'F' )
return c - 'A' + 10;
assert( false );
return 0xff;
}
inline char fromHex( const char *c ) {
return ( fromHex( c[ 0 ] ) << 4 ) | fromHex( c[ 1 ] );
}
}