Skip to content

Commit

Permalink
Optimize the ip2long function with trim logic applied
Browse files Browse the repository at this point in the history
  • Loading branch information
lionsoul2014 committed Oct 7, 2018
1 parent bd6dc7b commit 86104eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
45 changes: 25 additions & 20 deletions binding/c/ip2region.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* default ip2region implementation
*
* @see #ip2region.h
* @author chenxin<[email protected]>
* @see #ip2region.h
* @author chenxin<[email protected]>
* @date 2015-10-30
*/

Expand All @@ -13,7 +13,7 @@
/**
* create a new ip2region object
*
* @param dbFile path
* @param dbFile path
*/
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
{
Expand Down Expand Up @@ -51,7 +51,7 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
/**
* destroy the specifield ip2region object
*
* @param ip2region_t
* @param ip2region_t
*/
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
{
Expand Down Expand Up @@ -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
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
Expand Down Expand Up @@ -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
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
Expand Down Expand Up @@ -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
*
* @param buffer
* @param offset
* @return uint_t
* @param buffer
* @param offset
* @return uint_t
*/
IP2R_API uint_t getUnsignedInt(const char *buffer, int offset)
{
Expand All @@ -385,8 +385,8 @@ IP2R_API uint_t getUnsignedInt(const char *buffer, int offset)
/**
* string ip to long
*
* @param ip
* @return uint_t
* @param ip
* @return uint_t
*/
IP2R_API uint_t ip2long(const char *ip)
{
Expand All @@ -396,6 +396,11 @@ IP2R_API uint_t ip2long(const char *ip)
uint_t ipval = 0;

while ( *cs != '\0' ) {
if ( *cs == ' ' ) {
cs++;
continue;
}

if ( *cs == '.' ) {
//single part length limit
if ( i > 3 ) {
Expand Down Expand Up @@ -426,9 +431,9 @@ IP2R_API uint_t ip2long(const char *ip)
/**
* long to string ip
*
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
*/
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
{
Expand Down
6 changes: 3 additions & 3 deletions binding/c/testSearcher.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* test ip2region searcher program
*
* @author chenxin<[email protected]>
* @author chenxin<[email protected]>
* @date 2015-10-30
*/

Expand Down Expand Up @@ -49,7 +49,7 @@ int main( int argc, char **argv )
datablock_entry datablock;
char *dbFile = NULL, *algorithm = NULL;
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;
memset(&datablock, 0x00, sizeof(datablock_entry));

Expand Down Expand Up @@ -95,7 +95,7 @@ int main( int argc, char **argv )
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
}

//destory the ip2rObj
// destory the ip2rObj
ip2region_destroy(&ip2rEntry);

return 0;
Expand Down

0 comments on commit 86104eb

Please sign in to comment.