Given a multibyte string "x=π"
, how to get 4-th byte of the string,
without creating string copy with something like string-as-unibyte
?
Pseudo code: (string-nth-byte 4 "x=π")
.
Expected result: 128.
"x=π" = [120 61 207 128] <- bytes
0 1 2 3 <- index
Any solution that is reasonably efficient is considered valid.
O(1)
run time complexity is preferred.
If there is Emacs builtin for that, I will be very happy.
Notes:
- Allocations must be avoided;
get-byte
fails on multibyte chars;- I am aware of unibyte strings. They are not the answer;
If you ask: "Why on earth you need to do this?", the answer is this.
module
could help here because it is easier to deliver, but it will not provide good performance (calling C module function is very costly). Anyway, it is a solution actually. Probably I will try it out later. – Iskander Sharipov Jul 6 '17 at 8:01