From f424263a90990f5f5a21f077cc94ef8e3ea3b43d Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Thu, 7 May 2026 17:13:04 +0200 Subject: [PATCH 1/2] Use lookup table in parse_npy_datatype to avoid complex regex --- .Rbuildignore | 2 ++ DESCRIPTION | 2 ++ R/read_npy.R | 57 +++++++++++++------------------------ R/sysdata.rda | Bin 0 -> 407 bytes data-raw/supported_types.R | 32 +++++++++++++++++++++ 5 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 R/sysdata.rda create mode 100644 data-raw/supported_types.R 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 0000000000000000000000000000000000000000..d7aefc2d293d936454ff93b7bb11806fb98ac17b GIT binary patch literal 407 zcmV;I0cie0T4*^jL0KkKSz&-~pa2BO|G@w9$bbL>JVk{7mO#I^-XuT(00F=O+jO1X z&BSV_(wZKEG|yCRMvP4v8fY^} z5h$o1sGBHgJwc;D05l$^le?P1ObIZBTHr{OhO02DR;hwQgvtF1fwGcGVjxBsYgRzD zgBiV=s+TCFt!Pq`R=^*Tb7s@2Q(fW-068G;S1gcZkkEvOw?4aCm1$~~RihIC2{m(c zVyddD+_kQ%s;&SHi;{pcNd^WvA$(GWkZA=&I-n2$SV4-BQ9_Ybq!AO#O#yeJ06-Xb z`uyYLsKR<%fL9*JH4UXu?LD&sRB_o7ji{7P>^AOZlGSv Bt=Rwo literal 0 HcmV?d00001 diff --git a/data-raw/supported_types.R b/data-raw/supported_types.R new file mode 100644 index 0000000..966906b --- /dev/null +++ b/data-raw/supported_types.R @@ -0,0 +1,32 @@ +## 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"), + " Date: Thu, 7 May 2026 17:31:40 +0200 Subject: [PATCH 2/2] Use hashed env instead of list --- R/sysdata.rda | Bin 407 -> 408 bytes data-raw/supported_types.R | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/R/sysdata.rda b/R/sysdata.rda index d7aefc2d293d936454ff93b7bb11806fb98ac17b..ef9325e3900167e02a71ca99735eba591a1fc2d3 100644 GIT binary patch literal 408 zcmV;J0cZX~T4*^jL0KkKS*j+b;Q$3r|G@w9$N~TYJVk{7mO#I^-XuT(5COme+yd-F zDxa!o$)~7#gAinCkZF@>hK;CT02%-TCV&6~Kq93y4FCWD000^^2$4vW%6_T(Q+g=! znr%QD1JpDgr>2kgo5WKj(j7_^0Rk%^lI(EDVy2Z$tWd+hNc2n)!V0!91}87!B8Avv zj&Ow@QAu}IQmXYMt|8MdpSyt~!Hi@{B#?m=$pn2JhiSyN%PgXbJ^BD1$SQRK+XdyP z@@Tmzk%TFxA+%BoBJNar)gl-wW~oLBr9eU4s00K+h%jKJMFgm-)DaWu?Vw*200^lL z@7H%Oc0;_?WtJd7gSvqMzH3Ms@ehn}NZp8y$`4H}Sf zROi4bgp86BAO4!vGecq-0Po+vNinWFR6uR2VBZIfI4UwEVvrL0e;0B^I8czPCZ*w( CU#ax~ literal 407 zcmV;I0cie0T4*^jL0KkKSz&-~pa2BO|G@w9$bbL>JVk{7mO#I^-XuT(00F=O+jO1X z&BSV_(wZKEG|yCRMvP4v8fY^} z5h$o1sGBHgJwc;D05l$^le?P1ObIZBTHr{OhO02DR;hwQgvtF1fwGcGVjxBsYgRzD zgBiV=s+TCFt!Pq`R=^*Tb7s@2Q(fW-068G;S1gcZkkEvOw?4aCm1$~~RihIC2{m(c zVyddD+_kQ%s;&SHi;{pcNd^WvA$(GWkZA=&I-n2$SV4-BQ9_Ybq!AO#O#yeJ06-Xb z`uyYLsKR<%fL9*JH4UXu?LD&sRB_o7ji{7P>^AOZlGSv Bt=Rwo diff --git a/data-raw/supported_types.R b/data-raw/supported_types.R index 966906b..e44242c 100644 --- a/data-raw/supported_types.R +++ b/data-raw/supported_types.R @@ -27,6 +27,7 @@ supported_types <- list( nbytes = NA_integer_, endian = NA_character_ ) -) +) |> + list2env(hash = TRUE, parent = emptyenv()) usethis::use_data(supported_types, internal = TRUE, overwrite = TRUE)