Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New functions for bytes/strings #6500

Closed
vicuna opened this issue Jul 28, 2014 · 9 comments
Closed

New functions for bytes/strings #6500

vicuna opened this issue Jul 28, 2014 · 9 comments

Comments

@vicuna
Copy link

vicuna commented Jul 28, 2014

Original bug ID: 6500
Reporter: @alainfrisch
Status: feedback (set by @damiendoligez on 2014-08-20T08:24:44Z)
Resolution: open
Priority: normal
Severity: feature
Category: standard library
Tags: patch
Monitored by: braibant @whitequark

Bug description

Some useful functions to create string (esp. String.init, which avoids the need to go through a bytes than copy it):

String.init: int -> (int -> char) -> string
Bytes.init: int -> (int -> char) -> bytes

Some more useful functions to avoid copying between bytes and string, at least explicitly (this is much lower priority, I'd say):

Buffer.sub_bytes: t -> int -> int -> bytes
Format.print_bytes: bytes -> unit
Format.print_bytes_as: int -> bytes -> unit
Lexing.from_bytes: bytes -> lexbuf
Pervasives.print_bytes_endline: bytes -> unit
Pervasives.prerr_bytes_endline: bytes -> unit
Pervasives.read_line_bytes: unit -> bytes
Pervasives.input_line_bytes: in_channel -> bytes
Scanf.from_bytes: bytes -> in_channel
Stream.of_bytes : bytes -> char t

(There are a few more in Scanf, Printf.)

File attachments

@vicuna
Copy link
Author

vicuna commented Jul 28, 2014

Comment author: @alainfrisch

Commit 15029 on 4.02 (15030 on trunk) adds String.init, Bytes.init, Labels couterparts, Stream.of_byte.

@vicuna
Copy link
Author

vicuna commented Aug 2, 2014

Comment author: @gasche

The attached patch implements String.mapi, and Bytes.mapi in passing. If that is all right, I'd like to commit it eventually. I also specified the iteration order of init/map/mapi: in my experience users rely on it anyway, so we cannot change it, and it's better to not worry and just document it from the start.

I think the functions you listed are important and we should provide them to have a smooth transition. I can help with the implementation -- in any case I plan to document Bytes.unsafe_to_string and Bytes.unsafe_of_string, and try to minimize use of the latter.

@vicuna
Copy link
Author

vicuna commented Aug 6, 2014

Comment author: @alainfrisch

Gabriel: please commit mapi (also updating StringLabels/BytesLabels).

Also, the type bytes is really just an optimized implementation of "char array", and it thus makes sense to have the Bytes module implements all relevant operations from Array. The ones currently missing:

val to_list: bytes -> char list
val of_list: char list -> bytes
val fold_left: ('a -> char -> 'a) -> 'a -> bytes -> 'a
val fold_right: (char -> 'a -> 'a) -> 'bytes -> 'a -> 'a
val {sort, stable_sort, fast_sort}: (char -> char -> int) -> bytes -> unit

We should also consider renaming Bytes.cat to Bytes.append. Bytes.concat is a little bit problematic, since Array.concat also exists, but without the delimiter, and we want to keep the same name as String.

@vicuna
Copy link
Author

vicuna commented Aug 20, 2014

Comment author: @damiendoligez

Most of these function types don't make sense to me: a single "bytes" argument is pretty useless, you should almost always take three arguments: bytes, offset, length.

@vicuna
Copy link
Author

vicuna commented Sep 10, 2014

Comment author: kaustuv

Also worth adding: Format.pp_print_bytes. This should then also be a registered printer in the toplevel if it is invoked with -safe-string.

@yallop
Copy link
Member

yallop commented Apr 5, 2019

The standard library has String.init and Bytes.init, since 4.02.

@github-actions
Copy link

github-actions bot commented May 11, 2020

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@github-actions github-actions bot added the Stale label May 11, 2020
@whitequark
Copy link
Member

whitequark commented May 11, 2020

Still an issue.

@github-actions github-actions bot removed the Stale label Jun 15, 2020
avsm pushed a commit to avsm/ocaml that referenced this issue Jun 21, 2020
rixed added a commit to rixed/dessser that referenced this issue Nov 17, 2020
No more string copy, byt OCaml stdlib for Scanf.Scanning is lacking a
from_bytes :-(  (see ocaml/ocaml#6500)
@github-actions
Copy link

github-actions bot commented Jul 21, 2021

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants