src/string.c

changeset 1048
12f38affefd5
parent 1041
508dc8b32a17
child 1050
3df63e95921a
equal deleted inserted replaced
1047:40aad3f0bc9e 1048:12f38affefd5
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28 #define CX_STR_IMPLEMENTATION
29 #include "cx/string.h" 29 #include "cx/string.h"
30 30
31 #include <string.h> 31 #include <string.h>
32 #include <stdarg.h> 32 #include <stdarg.h>
33 #include <ctype.h> 33 #include <ctype.h>
34 #include <assert.h>
34 #include <errno.h> 35 #include <errno.h>
36 #include <limits.h>
35 37
36 #ifndef _WIN32 38 #ifndef _WIN32
37 39
38 #include <strings.h> // for strncasecmp() 40 #include <strings.h> // for strncasecmp()
39 41
813 size_t count 815 size_t count
814 ) { 816 ) {
815 ctx->delim_more = delim; 817 ctx->delim_more = delim;
816 ctx->delim_more_count = count; 818 ctx->delim_more_count = count;
817 } 819 }
820
821 #define cx_strtoX_signed_impl(rtype, rmin, rmax) \
822 int64_t result; \
823 if (cx_strtoi64_lc(str, &result, base, groupsep)) { \
824 return -1; \
825 } \
826 if (result < rmin || result > rmax) { \
827 errno = ERANGE; \
828 return -1; \
829 } \
830 *output = (rtype) result; \
831 return 0
832
833 int cx_strtos_lc(cxstring str, short *output, int base, const char *groupsep) {
834 cx_strtoX_signed_impl(short, SHRT_MIN, SHRT_MAX);
835 }
836
837 int cx_strtoi_lc(cxstring str, int *output, int base, const char *groupsep) {
838 cx_strtoX_signed_impl(int, INT_MIN, INT_MAX);
839 }
840
841 int cx_strtol_lc(cxstring str, long *output, int base, const char *groupsep) {
842 cx_strtoX_signed_impl(long, LONG_MIN, LONG_MAX);
843 }
844
845 int cx_strtoll_lc(cxstring str, long long *output, int base, const char *groupsep) {
846 assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms
847 return cx_strtoi64_lc(str, (int64_t*) output, base, groupsep);
848 }
849
850 int cx_strtoi8_lc(cxstring str, int8_t *output, int base, const char *groupsep) {
851 cx_strtoX_signed_impl(int8_t, INT8_MIN, INT8_MAX);
852 }
853
854 int cx_strtoi16_lc(cxstring str, int16_t *output, int base, const char *groupsep) {
855 cx_strtoX_signed_impl(int16_t, INT16_MIN, INT16_MAX);
856 }
857
858 int cx_strtoi32_lc(cxstring str, int32_t *output, int base, const char *groupsep) {
859 cx_strtoX_signed_impl(int32_t, INT32_MIN, INT32_MAX);
860 }
861
862 int cx_strtoi64_lc(cxstring str, int64_t *output, int base, const char *groupsep) {
863 // TODO: implement
864 return -1;
865 }
866
867 int cx_strtoz_lc(cxstring str, ssize_t *output, int base, const char *groupsep) {
868 #if CX_WORDSIZE == 32
869 return cx_strtoi32_lc(str, output, base, groupsep);
870 #else
871 return cx_strtoi64_lc(str, output, base, groupsep);
872 #endif
873 }
874
875 #define cx_strtoX_unsigned_impl(rtype, rmax) \
876 uint64_t result; \
877 if (cx_strtou64_lc(str, &result, base, groupsep)) { \
878 return -1; \
879 } \
880 if (result > rmax) { \
881 errno = ERANGE; \
882 return -1; \
883 } \
884 *output = (rtype) result; \
885 return 0
886
887 int cx_strtous_lc(cxstring str, unsigned short *output, int base, const char *groupsep) {
888 cx_strtoX_unsigned_impl(unsigned short, USHRT_MAX);
889 }
890
891 int cx_strtou_lc(cxstring str, unsigned int *output, int base, const char *groupsep) {
892 cx_strtoX_unsigned_impl(unsigned int, UINT_MAX);
893 }
894
895 int cx_strtoul_lc(cxstring str, unsigned long *output, int base, const char *groupsep) {
896 cx_strtoX_unsigned_impl(unsigned long, ULONG_MAX);
897 }
898
899 int cx_strtoull_lc(cxstring str, unsigned long long *output, int base, const char *groupsep) {
900 assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms
901 return cx_strtou64_lc(str, (uint64_t*) output, base, groupsep);
902 }
903
904 int cx_strtou8_lc(cxstring str, uint8_t *output, int base, const char *groupsep) {
905 cx_strtoX_unsigned_impl(uint8_t, UINT8_MAX);
906 }
907
908 int cx_strtou16_lc(cxstring str, uint16_t *output, int base, const char *groupsep) {
909 cx_strtoX_unsigned_impl(uint16_t, UINT16_MAX);
910 }
911
912 int cx_strtou32_lc(cxstring str, uint32_t *output, int base, const char *groupsep) {
913 cx_strtoX_unsigned_impl(uint32_t, UINT32_MAX);
914 }
915
916 int cx_strtou64_lc(cxstring str, uint64_t *output, int base, const char *groupsep) {
917 // TODO: implement
918 return -1;
919 }
920
921 int cx_strtouz_lc(cxstring str, size_t *output, int base, const char *groupsep) {
922 #if CX_WORDSIZE == 32
923 return cx_strtou32_lc(str, output, base, groupsep);
924 #else
925 return cx_strtou64_lc(str, output, base, groupsep);
926 #endif
927 }
928
929 int cx_strtof_lc(cxstring str, float *output, char decsep, const char *groupsep) {
930 // TODO: impl
931 return -1;
932 }
933
934 int cx_strtod_lc(cxstring str, double *output, char decsep, const char *groupsep) {
935 // TODO: impl
936 return -1;
937 }

mercurial