Skip to content

Commit 1076a67

Browse files
committed
misc compilation warning fixes
This makes pystring compile without warnings on gcc44, when compiled using the da -Wall flag. Most of the changes are issues related to the intermixing of size_type / int types, where the updated version of the code attempts to do as few type changes to the user arguments as possible. This code has been running cross-platform in OpenColorIO for the last few months, and is well tested.
1 parent 4e78522 commit 1076a67

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

pystring.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
///////////////////////////////////////////////////////////////////////////////
3333

34-
#include <ctype.h>
35-
#include <string.h>
36-
#include <iostream>
37-
#include <algorithm>
3834
#include "pystring.h"
3935

36+
#include <algorithm>
37+
#include <cctype>
38+
#include <cstring>
39+
#include <iostream>
40+
4041
namespace pystring
4142
{
4243

@@ -397,13 +398,14 @@ namespace pystring
397398
//////////////////////////////////////////////////////////////////////////////////////////////
398399
///
399400
///
400-
int __adjustslicepos( std::string::size_type len, int pos )
401+
int __adjustslicepos( std::string::size_type len_, int pos )
401402
{
402-
int intlen = len, value;
403+
int len = static_cast<int>(len_);
404+
int value;
403405

404406
if ( pos < 0 )
405407
{
406-
value = intlen + pos;
408+
value = len + pos;
407409
}
408410
else
409411
{
@@ -415,7 +417,7 @@ namespace pystring
415417
value = 0;
416418
}
417419

418-
else if ( value > ( int ) len )
420+
else if ( value > len)
419421
{
420422
value = len;
421423
}
@@ -431,14 +433,14 @@ namespace pystring
431433
///
432434
bool startswith( const std::string & str, const std::string & prefix, int start, int end )
433435
{
434-
std::string::size_type startp, endp;
435-
436+
int startp, endp;
437+
436438
startp = __adjustslicepos( str.size(), start );
437439
endp = __adjustslicepos( str.size(), end );
438440

439441
if ( start > (int) str.size() ) return false;
440442

441-
if ( endp - startp < prefix.size() ) return false;
443+
if ( endp - startp < (int) prefix.size() ) return false;
442444
return __substrcmp( str, prefix, startp );
443445

444446
}
@@ -448,18 +450,19 @@ namespace pystring
448450
///
449451
bool endswith( const std::string & str, const std::string & suffix, int start, int end )
450452
{
451-
std::string::size_type startp, endp;
453+
int startp, endp;
452454

453455
startp = __adjustslicepos( str.size(), start );
454456
endp = __adjustslicepos( str.size(), end );
455-
457+
458+
int suffixsize = (int) suffix.size();
456459
int upper = endp;
457-
int lower = ( upper - suffix.size() ) > startp ? ( upper - suffix.size() ) : startp;
460+
int lower = ( upper - suffixsize ) > startp ? ( upper - suffixsize ) : startp;
458461

459462
if ( start > (int) str.size() ) return false;
460463

461464

462-
if ( upper - lower < ( int ) suffix.size() )
465+
if ( upper - lower < suffixsize )
463466
{
464467
return false;
465468
}
@@ -624,12 +627,12 @@ namespace pystring
624627

625628
if ( len > 0)
626629
{
627-
if (::islower(s[0])) s[0] = ::toupper( s[0] );
630+
if (::islower(s[0])) s[0] = (char) ::toupper( s[0] );
628631
}
629632

630633
for ( i = 1; i < len; ++i )
631634
{
632-
if (::isupper(s[i])) s[i] = ::tolower( s[i] );
635+
if (::isupper(s[i])) s[i] = (char) ::tolower( s[i] );
633636
}
634637

635638
return s;
@@ -645,7 +648,7 @@ namespace pystring
645648

646649
for ( i = 0; i < len; ++i )
647650
{
648-
if ( ::isupper( s[i] ) ) s[i] = ::tolower( s[i] );
651+
if ( ::isupper( s[i] ) ) s[i] = (char) ::tolower( s[i] );
649652
}
650653

651654
return s;
@@ -661,7 +664,7 @@ namespace pystring
661664

662665
for ( i = 0; i < len; ++i )
663666
{
664-
if ( ::islower( s[i] ) ) s[i] = ::toupper( s[i] );
667+
if ( ::islower( s[i] ) ) s[i] = (char) ::toupper( s[i] );
665668
}
666669

667670
return s;
@@ -677,8 +680,8 @@ namespace pystring
677680

678681
for ( i = 0; i < len; ++i )
679682
{
680-
if ( ::islower( s[i] ) ) s[i] = ::toupper( s[i] );
681-
else if (::isupper( s[i] ) ) s[i] = ::tolower( s[i] );
683+
if ( ::islower( s[i] ) ) s[i] = (char) ::toupper( s[i] );
684+
else if (::isupper( s[i] ) ) s[i] = (char) ::tolower( s[i] );
682685
}
683686

684687
return s;
@@ -700,15 +703,15 @@ namespace pystring
700703
{
701704
if ( !previous_is_cased )
702705
{
703-
s[i] = ::toupper(c);
706+
s[i] = (char) ::toupper(c);
704707
}
705708
previous_is_cased = true;
706709
}
707710
else if ( ::isupper(c) )
708711
{
709712
if ( previous_is_cased )
710713
{
711-
s[i] = ::tolower(c);
714+
s[i] = (char) ::tolower(c);
712715
}
713716
previous_is_cased = true;
714717
}
@@ -776,9 +779,9 @@ namespace pystring
776779
///
777780
std::string zfill( const std::string & str, int width )
778781
{
779-
std::string::size_type len = str.size();
782+
int len = (int)str.size();
780783

781-
if ( ( int ) len >= width )
784+
if ( len >= width )
782785
{
783786
return str;
784787
}
@@ -825,10 +828,10 @@ namespace pystring
825828
///
826829
std::string center( const std::string & str, int width )
827830
{
828-
std::string::size_type len = str.size();
831+
int len = (int) str.size();
829832
int marg, left;
830833

831-
if ( (( int ) len ) >= width ) return str;
834+
if ( len >= width ) return str;
832835

833836
marg = width - len;
834837
left = marg / 2 + (marg & width & 1);
@@ -842,7 +845,7 @@ namespace pystring
842845
///
843846
int find( const std::string & str, const std::string & sub, int start, int end )
844847
{
845-
std::string::size_type startp, endp;
848+
int startp, endp;
846849

847850
startp = __adjustslicepos( str.size(), start );
848851
endp = __adjustslicepos( str.size(), end );
@@ -851,12 +854,12 @@ namespace pystring
851854

852855
result = str.find( sub, startp );
853856

854-
if( result == std::string::npos || result >= endp)
857+
if( result == std::string::npos || result >= (std::string::size_type)endp)
855858
{
856859
return -1;
857860
}
858861

859-
return result;
862+
return (int)result;
860863
}
861864

862865
//////////////////////////////////////////////////////////////////////////////////////////////
@@ -872,7 +875,7 @@ namespace pystring
872875
///
873876
int rfind( const std::string & str, const std::string & sub, int start, int end )
874877
{
875-
std::string::size_type startp, endp;
878+
int startp, endp;
876879

877880
startp = __adjustslicepos( str.size(), start );
878881
endp = __adjustslicepos( str.size(), end );
@@ -881,9 +884,10 @@ namespace pystring
881884

882885
result = str.rfind( sub, endp );
883886

884-
if( result == std::string::npos || result < startp ) return -1;
885-
return result;
886-
887+
if( result == std::string::npos || result < (std::string::size_type)startp )
888+
return -1;
889+
890+
return (int)result;
887891
}
888892

889893
//////////////////////////////////////////////////////////////////////////////////////////////
@@ -953,7 +957,7 @@ namespace pystring
953957

954958
if ( cursor < 0 ) break;
955959

956-
cursor += substr.size();
960+
cursor += (int) substr.size();
957961
nummatches += 1;
958962
}
959963

@@ -982,7 +986,7 @@ namespace pystring
982986

983987
s.replace( cursor, oldlen, newstr );
984988

985-
cursor += newlen;
989+
cursor += (int) newlen;
986990
++sofar;
987991
}
988992

@@ -1035,7 +1039,7 @@ namespace pystring
10351039
///
10361040
std::string slice( const std::string & str, int start, int end )
10371041
{
1038-
std::string::size_type startp, endp;
1042+
int startp, endp;
10391043

10401044
startp = __adjustslicepos( str.size(), start );
10411045
endp = __adjustslicepos( str.size(), end );

0 commit comments

Comments
 (0)