caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* index of substring
@ 2005-01-27  0:41 yjc01
  2005-01-27  4:21 ` [Caml-list] " Matt Gushee
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: yjc01 @ 2005-01-27  0:41 UTC (permalink / raw)
  To: caml-list

I tried to write a function to return the index of a substring in a string from
an index. So I can use it to extract some comments (text between (* and *) in a
file. But came across some problem when returning the value.

The code I came up with is: 
(* Use Unix Module *)
open Unix;;


(* get the file size *)
let fileReader = openfile "student.cd" [O_RDONLY] 0o640;;
let fileSize = (fstat fileReader).st_size;;
print_int fileSize;;

(* create variable to store file *)
let comments = String.make fileSize 'c';;

 
let read_char () = 
	read fileReader comments 0 fileSize;;


(* return index of a substring in a string from an index, if substring not
matched retun -1 *)
let index_left fromIndex string findString = 
	(* Step 1. Find first character *)
	let firstIndex = String.index_from string fromIndex (String.get findString 0) in
	
	   if (firstIndex >= 0)	then
		let isMatched = ref true in
		for i = 1 to (String.length findString) do
			if (String.get string firstIndex) != (String.get findString 0) 
			   then !isMatched = false;
			if (!isMatched)
			   then firstIndex 
			   else -1;
			
		done;;
				
(* test if index_left works *)
let check index = 
	 index_left index comments "(*" 
		

print_int check 1;;


Or is there an easier way to do this?

Daisy


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2005-01-28 13:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-27  0:41 index of substring yjc01
2005-01-27  4:21 ` [Caml-list] " Matt Gushee
2005-01-27  6:44 ` Radu Grigore
2005-01-27  9:36   ` skaller
2005-01-27 10:20     ` Jean-Christophe Filliatre
2005-01-27 16:55       ` skaller
2005-01-27 17:20         ` Radu Grigore
2005-01-28  8:51         ` Jean-Christophe Filliatre
2005-01-28 13:31           ` skaller
2005-01-27  8:37 ` Oliver Bandel
2005-01-27  9:17 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).