Skip to content

Commit 2c20b56

Browse files
committed
Add arbitrary-base string-to-int conversion and more error checking
1 parent 1102733 commit 2c20b56

File tree

4 files changed

+314
-77
lines changed

4 files changed

+314
-77
lines changed

src/flc_string.i

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,35 @@ class string {
122122
}
123123
}
124124

125-
// String conversion
126-
int stoi(const std::string& s);
127-
long stol(const std::string& s);
128-
long long stoll(const std::string& s);
129-
float stof(const std::string& s);
130-
double stod(const std::string& s);
125+
%fragment("flc_has_junk", "header", fragment="<cctype>", fragment="<algorithm>") %{
126+
SWIGINTERN bool flc_has_junk(const std::string& s, size_t pos) {
127+
return !std::all_of(s.begin() + pos, s.end(), std::isspace);
128+
}
129+
%}
130+
131+
%typemap(in, numinputs=0, noblock=1) size_t* result_pos (size_t temp_pos) {
132+
temp_pos = 0;
133+
$1 = &temp_pos;
134+
}
135+
%typemap(argout, noblock=1, fragment="flc_has_junk") size_t* result_pos {
136+
if (flc_has_junk(*arg1, temp_pos$argnum)) {
137+
SWIG_exception(SWIG_ValueError, "Junk at end of string");
138+
}
139+
}
140+
141+
// String conversion routines
142+
#define %add_string_int_conversion(RETURN_TYPE, NAME) \
143+
RETURN_TYPE NAME(const string& s, size_t* result_pos, int base = 10)
144+
#define %add_string_real_conversion(RETURN_TYPE, NAME) \
145+
RETURN_TYPE NAME(const string& s, size_t* result_pos)
146+
147+
%add_string_int_conversion(int, stoi);
148+
%add_string_int_conversion(long, stol);
149+
%add_string_int_conversion(long long, stoll);
150+
%add_string_real_conversion(float, stof);
151+
%add_string_real_conversion(double, stod);
152+
153+
// Don't add exception code later
154+
%exception;
131155

132156
} // namespace std

src/generated/flc_string.f90

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,20 @@ module flc_string
5959
module procedure swigf_new_string__SWIG_1
6060
module procedure swigf_new_string__SWIG_2
6161
end interface
62-
public :: stoi
63-
public :: stol
64-
public :: stoll
6562
public :: stof
6663
public :: stod
64+
interface stoi
65+
module procedure swigf_stoi__SWIG_0, swigf_stoi__SWIG_1
66+
end interface
67+
public :: stoi
68+
interface stoll
69+
module procedure swigf_stoll__SWIG_0, swigf_stoll__SWIG_1
70+
end interface
71+
public :: stoll
72+
interface stol
73+
module procedure swigf_stol__SWIG_0, swigf_stol__SWIG_1
74+
end interface
75+
public :: stol
6776

6877
! WRAPPER DECLARATIONS
6978
interface
@@ -275,26 +284,56 @@ subroutine swigc_string_op_assign__(farg1, farg2) &
275284
type(SwigClassWrapper) :: farg2
276285
end subroutine
277286

278-
function swigc_stoi(farg1) &
279-
bind(C, name="_wrap_stoi") &
287+
function swigc_stoi__SWIG_0(farg1, farg3) &
288+
bind(C, name="_wrap_stoi__SWIG_0") &
280289
result(fresult)
281290
use, intrinsic :: ISO_C_BINDING
282291
import :: swigarraywrapper
283292
type(SwigArrayWrapper) :: farg1
293+
integer(C_INT), intent(in) :: farg3
284294
integer(C_INT) :: fresult
285295
end function
286296

287-
function swigc_stol(farg1) &
288-
bind(C, name="_wrap_stol") &
297+
function swigc_stoi__SWIG_1(farg1) &
298+
bind(C, name="_wrap_stoi__SWIG_1") &
289299
result(fresult)
290300
use, intrinsic :: ISO_C_BINDING
291301
import :: swigarraywrapper
292302
type(SwigArrayWrapper) :: farg1
303+
integer(C_INT) :: fresult
304+
end function
305+
306+
function swigc_stol__SWIG_0(farg1, farg3) &
307+
bind(C, name="_wrap_stol__SWIG_0") &
308+
result(fresult)
309+
use, intrinsic :: ISO_C_BINDING
310+
import :: swigarraywrapper
311+
type(SwigArrayWrapper) :: farg1
312+
integer(C_INT), intent(in) :: farg3
293313
integer(C_LONG) :: fresult
294314
end function
295315

296-
function swigc_stoll(farg1) &
297-
bind(C, name="_wrap_stoll") &
316+
function swigc_stol__SWIG_1(farg1) &
317+
bind(C, name="_wrap_stol__SWIG_1") &
318+
result(fresult)
319+
use, intrinsic :: ISO_C_BINDING
320+
import :: swigarraywrapper
321+
type(SwigArrayWrapper) :: farg1
322+
integer(C_LONG) :: fresult
323+
end function
324+
325+
function swigc_stoll__SWIG_0(farg1, farg3) &
326+
bind(C, name="_wrap_stoll__SWIG_0") &
327+
result(fresult)
328+
use, intrinsic :: ISO_C_BINDING
329+
import :: swigarraywrapper
330+
type(SwigArrayWrapper) :: farg1
331+
integer(C_INT), intent(in) :: farg3
332+
integer(C_LONG_LONG) :: fresult
333+
end function
334+
335+
function swigc_stoll__SWIG_1(farg1) &
336+
bind(C, name="_wrap_stoll__SWIG_1") &
298337
result(fresult)
299338
use, intrinsic :: ISO_C_BINDING
300339
import :: swigarraywrapper
@@ -693,7 +732,24 @@ subroutine swigf_string_op_assign__(self, other)
693732
self%swigdata = farg1
694733
end subroutine
695734

696-
function stoi(s) &
735+
function swigf_stoi__SWIG_0(s, base) &
736+
result(swig_result)
737+
use, intrinsic :: ISO_C_BINDING
738+
integer(C_INT) :: swig_result
739+
character(kind=C_CHAR, len=*), target :: s
740+
integer(C_INT), intent(in) :: base
741+
integer(C_INT) :: fresult
742+
character(kind=C_CHAR), dimension(:), allocatable, target :: farg1_temp
743+
type(SwigArrayWrapper) :: farg1
744+
integer(C_INT) :: farg3
745+
746+
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
747+
farg3 = base
748+
fresult = swigc_stoi__SWIG_0(farg1, farg3)
749+
swig_result = fresult
750+
end function
751+
752+
function swigf_stoi__SWIG_1(s) &
697753
result(swig_result)
698754
use, intrinsic :: ISO_C_BINDING
699755
integer(C_INT) :: swig_result
@@ -703,25 +759,59 @@ function stoi(s) &
703759
type(SwigArrayWrapper) :: farg1
704760

705761
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
706-
fresult = swigc_stoi(farg1)
762+
fresult = swigc_stoi__SWIG_1(farg1)
707763
swig_result = fresult
708764
end function
709765

710-
function stol(s) &
766+
function swigf_stol__SWIG_0(s, base) &
711767
result(swig_result)
712768
use, intrinsic :: ISO_C_BINDING
713769
integer(C_LONG) :: swig_result
714770
character(kind=C_CHAR, len=*), target :: s
771+
integer(C_INT), intent(in) :: base
715772
integer(C_LONG) :: fresult
716773
character(kind=C_CHAR), dimension(:), allocatable, target :: farg1_temp
717774
type(SwigArrayWrapper) :: farg1
775+
integer(C_INT) :: farg3
776+
777+
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
778+
farg3 = base
779+
fresult = swigc_stol__SWIG_0(farg1, farg3)
780+
swig_result = fresult
781+
end function
782+
783+
function swigf_stol__SWIG_1(s) &
784+
result(swig_result)
785+
use, intrinsic :: ISO_C_BINDING
786+
integer(C_LONG) :: swig_result
787+
character(kind=C_CHAR, len=*), target :: s
788+
integer(C_LONG) :: fresult
789+
character(kind=C_CHAR), dimension(:), allocatable, target :: farg1_temp
790+
type(SwigArrayWrapper) :: farg1
791+
792+
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
793+
fresult = swigc_stol__SWIG_1(farg1)
794+
swig_result = fresult
795+
end function
796+
797+
function swigf_stoll__SWIG_0(s, base) &
798+
result(swig_result)
799+
use, intrinsic :: ISO_C_BINDING
800+
integer(C_LONG_LONG) :: swig_result
801+
character(kind=C_CHAR, len=*), target :: s
802+
integer(C_INT), intent(in) :: base
803+
integer(C_LONG_LONG) :: fresult
804+
character(kind=C_CHAR), dimension(:), allocatable, target :: farg1_temp
805+
type(SwigArrayWrapper) :: farg1
806+
integer(C_INT) :: farg3
718807

719808
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
720-
fresult = swigc_stol(farg1)
809+
farg3 = base
810+
fresult = swigc_stoll__SWIG_0(farg1, farg3)
721811
swig_result = fresult
722812
end function
723813

724-
function stoll(s) &
814+
function swigf_stoll__SWIG_1(s) &
725815
result(swig_result)
726816
use, intrinsic :: ISO_C_BINDING
727817
integer(C_LONG_LONG) :: swig_result
@@ -731,7 +821,7 @@ function stoll(s) &
731821
type(SwigArrayWrapper) :: farg1
732822

733823
call SWIGTM_fin_const_SS_char_Sm_(s, farg1, farg1_temp)
734-
fresult = swigc_stoll(farg1)
824+
fresult = swigc_stoll__SWIG_1(farg1)
735825
swig_result = fresult
736826
end function
737827

0 commit comments

Comments
 (0)