Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2008-01-22 (08:12)
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 ...



(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)