Version française
Home     About     Download     Resources     Contact us    
Browse thread
"Safe" unsafe string access
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Allsopp <dra-news@m...>
Subject: "Safe" unsafe string access
Without inviting any opinions on the *sensibility* of doing this, given the
internal representation of the empty string in OCaml, could I take

String.unsafe_get str 0

to be safe for any string? In the empty-string case, I'm thinking it will
always return '\000' accessing the null terminator of the empty string which
is fine as long as I'm not testing for a real '\000' in a string!

My motivation comes from parsing code that test the first character of a
string. Normally, for example, I'd write:

if s <> "" && s.[0] = '$'
then ...

But I'm wondering whether I can safely change this to:

If String.unsafe_get s 0 = '$'
then ...

Correct?


David

(Normally I would never use unsafe_ functions unless writing a library
function where I have demonstrably already performed the necessary check
that the safe version of the function would perform)