Optimize the ip2long function with trim logic applied

This commit is contained in:
lionsoul 2018-10-07 10:23:17 +08:00
parent 835d9c1b90
commit d796a76400
2 changed files with 28 additions and 23 deletions

View File

@ -1,8 +1,8 @@
/** /**
* default ip2region implementation * default ip2region implementation
* *
* @see #ip2region.h * @see #ip2region.h
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30 * @date 2015-10-30
*/ */
@ -13,7 +13,7 @@
/** /**
* create a new ip2region object * create a new ip2region object
* *
* @param dbFile path * @param dbFile path
*/ */
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile) IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
{ {
@ -51,7 +51,7 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
/** /**
* destroy the specifield ip2region object * destroy the specifield ip2region object
* *
* @param ip2region_t * @param ip2region_t
*/ */
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj) IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
{ {
@ -162,10 +162,10 @@ IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, const char *
/** /**
* get the region associated with the specifield ip address with binary search algorithm * get the region associated with the specifield ip address with binary search algorithm
* *
* @param ip2rObj * @param ip2rObj
* @param ip * @param ip
* @param datablock * @param datablock
* @return uint_t * @return uint_t
*/ */
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock) IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{ {
@ -239,10 +239,10 @@ IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, const char *
/** /**
* get the region associated with the specifield ip address with b-tree algorithm * get the region associated with the specifield ip address with b-tree algorithm
* *
* @param ip2rObj * @param ip2rObj
* @param ip * @param ip
* @param datablock * @param datablock
* @return uint_t * @return uint_t
*/ */
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock) IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{ {
@ -368,9 +368,9 @@ IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, const char *i
/** /**
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset * get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
* *
* @param buffer * @param buffer
* @param offset * @param offset
* @return uint_t * @return uint_t
*/ */
IP2R_API uint_t getUnsignedInt(const char *buffer, int offset) IP2R_API uint_t getUnsignedInt(const char *buffer, int offset)
{ {
@ -385,8 +385,8 @@ IP2R_API uint_t getUnsignedInt(const char *buffer, int offset)
/** /**
* string ip to long * string ip to long
* *
* @param ip * @param ip
* @return uint_t * @return uint_t
*/ */
IP2R_API uint_t ip2long(const char *ip) IP2R_API uint_t ip2long(const char *ip)
{ {
@ -396,6 +396,11 @@ IP2R_API uint_t ip2long(const char *ip)
uint_t ipval = 0; uint_t ipval = 0;
while ( *cs != '\0' ) { while ( *cs != '\0' ) {
if ( *cs == ' ' ) {
cs++;
continue;
}
if ( *cs == '.' ) { if ( *cs == '.' ) {
//single part length limit //single part length limit
if ( i > 3 ) { if ( i > 3 ) {
@ -426,9 +431,9 @@ IP2R_API uint_t ip2long(const char *ip)
/** /**
* long to string ip * long to string ip
* *
* @param ip * @param ip
* @param buffer * @param buffer
* @return uint_t(1 for success and 0 for failed) * @return uint_t(1 for success and 0 for failed)
*/ */
IP2R_API uint_t long2ip(uint_t ip, char *buffer) IP2R_API uint_t long2ip(uint_t ip, char *buffer)
{ {

View File

@ -1,7 +1,7 @@
/** /**
* test ip2region searcher program * test ip2region searcher program
* *
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30 * @date 2015-10-30
*/ */
@ -49,7 +49,7 @@ int main( int argc, char **argv )
datablock_entry datablock; datablock_entry datablock;
char *dbFile = NULL, *algorithm = NULL; char *dbFile = NULL, *algorithm = NULL;
char line[256]; char line[256];
uint_t (*func_ptr)(ip2region_t, char *, datablock_t); uint_t (*func_ptr)(ip2region_t, const char *, datablock_t);
double s_time, c_time; double s_time, c_time;
memset(&datablock, 0x00, sizeof(datablock_entry)); memset(&datablock, 0x00, sizeof(datablock_entry));
@ -95,7 +95,7 @@ int main( int argc, char **argv )
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time); printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
} }
//destory the ip2rObj // destory the ip2rObj
ip2region_destroy(&ip2rEntry); ip2region_destroy(&ip2rEntry);
return 0; return 0;