diff --git a/.Rbuildignore b/.Rbuildignore index 74dabe4..0d85a60 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,5 @@ ^touchstone$ ^\.lintr$ ^cran-comments\.md$ +^CRAN-SUBMISSION$ +^data-raw$ diff --git a/DESCRIPTION b/DESCRIPTION index 841f04c..3bbea42 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,3 +25,5 @@ BugReports: https://github.com/Bisaloo/grumpy/issues Imports: jsonlite Config/roxygen2/version: 8.0.0 +Depends: + R (>= 3.5) diff --git a/R/read_npy.R b/R/read_npy.R index ba0b16e..78a14fc 100644 --- a/R/read_npy.R +++ b/R/read_npy.R @@ -123,52 +123,35 @@ parse_npy_datatype <- function(descr) { ) ) } - descr_components <- regmatches( - descr, - regexec("^([<>|]?)([a-zA-Z])([0-9]*)$", descr) - )[[1L]] - endian <- if (descr_components[2L] %in% c("", "|")) { - NA_character_ - } else { - switch( - descr_components[2L], - `<` = "little", - `>` = "big", - stop( - "Invalid endianness: ", - descr_components[1L], - call. = FALSE + if (startsWith(descr, "|S")) { + return( + list( + endian = NA_character_, + base_type = "string", + nbytes = as.integer(sub("|S", "", descr, fixed = TRUE)) + ) + ) + } + if (startsWith(descr, "U")) { + charlen <- as.integer(sub("^[<>]U", "", descr)) + return( + list( + endian = if (startsWith(descr, "<")) "little" else "big", + base_type = "unicode", + nbytes = charlen * 4L ) ) } - python_type <- descr_components[3L] - n <- as.integer(descr_components[4L]) - - type_map <- list( - f = list(base_type = "float", size = n), - i = list(base_type = "int", size = n), - u = list(base_type = "uint", size = n), - `?` = list(base_type = "bool", size = 1L), - b = list(base_type = "bool", size = 1L), - a = list(base_type = "string", size = n), - S = list(base_type = "string", size = n), - U = list(base_type = "unicode", size = n * 4L), - c = list(base_type = "complex", size = n), - m = list(base_type = "timedelta", size = n), - M = list(base_type = "datetime", size = n), - V = list(base_type = "other", size = n), - O = list(base_type = "py_object", size = NA_integer_) - ) - entry <- type_map[[python_type]] + entry <- supported_types[[descr]] if (is.null(entry)) { - stop("Unsupported data type: ", descr_components[1L], call. = FALSE) + stop("Unsupported data type: ", descr, call. = FALSE) } return(list( - endian = endian, + endian = entry$endian, base_type = entry$base_type, - nbytes = entry$size + nbytes = entry$nbytes )) } diff --git a/R/sysdata.rda b/R/sysdata.rda new file mode 100644 index 0000000..ef9325e Binary files /dev/null and b/R/sysdata.rda differ diff --git a/data-raw/supported_types.R b/data-raw/supported_types.R new file mode 100644 index 0000000..e44242c --- /dev/null +++ b/data-raw/supported_types.R @@ -0,0 +1,33 @@ +## code to prepare `supported_types` dataset goes here +supported_types <- list( + ">f2" = list(base_type = "float", nbytes = 2L, endian = "big"), + "f4" = list(base_type = "float", nbytes = 4L, endian = "big"), + "f8" = list(base_type = "float", nbytes = 8L, endian = "big"), + "i2" = list(base_type = "int", nbytes = 2L, endian = "big"), + "i4" = list(base_type = "int", nbytes = 4L, endian = "big"), + "i8" = list(base_type = "int", nbytes = 8L, endian = "big"), + "u2" = list(base_type = "uint", nbytes = 2L, endian = "big"), + "u4" = list(base_type = "uint", nbytes = 4L, endian = "big"), + "u8" = list(base_type = "uint", nbytes = 8L, endian = "big"), + " + list2env(hash = TRUE, parent = emptyenv()) + +usethis::use_data(supported_types, internal = TRUE, overwrite = TRUE)