[Rcpp-commits] r3332 - in pkg/int64: inst/include inst/include/int64 src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 12 14:46:21 CET 2011


Author: romain
Date: 2011-11-12 14:46:21 +0100 (Sat, 12 Nov 2011)
New Revision: 3332

Added:
   pkg/int64/inst/include/int64/read_string_forward.h
Modified:
   pkg/int64/inst/include/int64.h
   pkg/int64/inst/include/int64/int64.h
   pkg/int64/inst/include/int64/read_string.h
   pkg/int64/src/int64.cpp
Log:
deal with overflow when converting character values to [u]int64

Modified: pkg/int64/inst/include/int64/int64.h
===================================================================
--- pkg/int64/inst/include/int64/int64.h	2011-11-12 12:12:38 UTC (rev 3331)
+++ pkg/int64/inst/include/int64/int64.h	2011-11-12 13:46:21 UTC (rev 3332)
@@ -27,11 +27,12 @@
     }
 }
 
+#include <int64/read_string_forward.h>
 #include <int64/get_long.h>
 #include <int64/get_bits.h>
 #include <int64/get_class.h>
+#include <int64/LongVector.h>
 #include <int64/read_string.h>
-#include <int64/LongVector.h>
 #include <int64/format_binary.h>
 #include <int64/as_long.h>
 #include <int64/as_character.h>

Modified: pkg/int64/inst/include/int64/read_string.h
===================================================================
--- pkg/int64/inst/include/int64/read_string.h	2011-11-12 12:12:38 UTC (rev 3331)
+++ pkg/int64/inst/include/int64/read_string.h	2011-11-12 13:46:21 UTC (rev 3332)
@@ -23,18 +23,21 @@
               
 namespace int64{
     namespace internal{
-
-    template <typename LONG>
-    inline LONG read_string(const char* s) ;
-    
+ 
     template <>
     inline int64_t read_string<int64_t>(const char* s ){
-        return strtoll( s, NULL, 0 ) ; 
+        errno = 0 ;
+        int64_t res = strtoll( s, NULL, 0 ) ;
+        if( errno == ERANGE ) res = int64::LongVector<int64_t>::na ;
+        return res ;
     }
         
     template <>
     inline uint64_t read_string<uint64_t>(const char* s){
-        return strtoull( s, NULL, 0 ) ;
+        errno = 0 ;
+        uint64_t res = strtoull( s, NULL, 0 ) ;
+        if( errno == ERANGE ) res = int64::LongVector<uint64_t>::na ;
+        return res ;
     } 
         
     

Added: pkg/int64/inst/include/int64/read_string_forward.h
===================================================================
--- pkg/int64/inst/include/int64/read_string_forward.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/read_string_forward.h	2011-11-12 13:46:21 UTC (rev 3332)
@@ -0,0 +1,33 @@
+// read_string_forward.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google Inc.  All rights reserved.
+//
+// This file is part of int64.
+//
+// int64 is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// int64 is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License  
+// along with int64.  If not, see <http://www.gnu.org/licenses/>.    
+    
+#ifndef int64__read_string_forward_h
+#define int64__read_string_forward_h
+              
+namespace int64{
+    namespace internal{
+
+    template <typename LONG>
+    inline LONG read_string(const char* s) ;
+        
+    } // namespace internal
+} // namespace int64
+
+#endif

Modified: pkg/int64/inst/include/int64.h
===================================================================
--- pkg/int64/inst/include/int64.h	2011-11-12 12:12:38 UTC (rev 3331)
+++ pkg/int64/inst/include/int64.h	2011-11-12 13:46:21 UTC (rev 3332)
@@ -27,6 +27,7 @@
 #include <stdint.h>		// replace with cstdint if newer C++ standard used
 #include <limits>		// for numeric_limits
 #include <string>
+#include <errno.h>
 
 #include <int64/int64.h>
 

Modified: pkg/int64/src/int64.cpp
===================================================================
--- pkg/int64/src/int64.cpp	2011-11-12 12:12:38 UTC (rev 3331)
+++ pkg/int64/src/int64.cpp	2011-11-12 13:46:21 UTC (rev 3332)
@@ -196,7 +196,7 @@
     
     SEXP res = PROTECT( Rf_allocVector( STRSXP, n ) ) ;
     for( int i=0; i<n; i++){
-        if( !strcmp( CHAR(STRING_ELT(x_, i)), "NA" ) || digits[i] > len[i] ){
+        if( !strcmp( CHAR(STRING_ELT(s_, i)), "NA" ) || digits[i] > len[i] ){
             SET_STRING_ELT( res, i, STRING_ELT( s_, i ) ) ;    
         } else {
             s = CHAR(STRING_ELT(s_, i ));



More information about the Rcpp-commits mailing list