123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Anton Lavrik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)moduleU=Piqi_utilopenU.Stdlethas_parentname=String.containsname'/'(* analogous to Filename.dirname *)letget_module_namex=tryletpos=String.rindexx'/'inletres=String.subx0posinSomereswithNot_found->None(* analogous to Filename.basename *)letget_local_namex=tryletpos=String.rindexx'/'inString.subx(pos+1)((String.lengthx)-pos-1)withNot_found->xletis_scoped_namename=String.containsname'/'letsplit_namex=get_module_namex,get_local_namexletis_valid_charallow=function|'a'..'z'|'A'..'Z'|'0'..'9'|'-'->true|x->String.containsallowxletis_valid_name?(allow="")x=letlen=String.lengthxinletreccheck_chari=ifi>=lenthentrueelseifis_valid_charallowx.[i]thencheck_char(i+1)elsefalseiniflen=0thenfalseelsematchx.[0]with|'a'..'z'|'A'..'Z'->check_char1|_->falseletsomef=function|None->true|Somex->fxletis_valid_pathnamex=(* TODO: check domain name and URL/paths validity *)is_valid_namex~allow:"/._"letis_valid_modnamex=letdirname,basename=split_namexin(* NOTE: allowing underscores and dots in basename *)someis_valid_pathnamedirname&&is_valid_namebasename~allow:"_."letis_valid_typename?allowx=letmodname,typename=split_namexinsomeis_valid_modnamemodname&&is_valid_nametypename?allowletnormalize_listl=letisupperc=(c>='A'&&c<='Z')inlettolowerc=Char.chr(Char.codec+32)inletrecauxhumpaccu=function|[]->List.revaccu|h::twhenh='_'||h='-'->auxtrue('-'::accu)t|h::twhenisupperh&¬hump->(* first hump character *)auxtrue((tolowerh)::'-'::accu)t|h::twhenisupperh&&hump->(* another hump character *)auxhump((tolowerh)::accu)t|h::twhenh='.'||h=':'||h='/'->auxtrue(h::accu)t|h::t->(* end of hump *)auxfalse(h::accu)tinmatchlwith|[]->[]|h::_->aux(isupperh)[]l(* check if the name is normal, i.e. no uppercase characters and no hyphens *)letis_normal_names=letlen=String.lengthsinletrecauxi=ifi=lenthentrue(* the name is normal *)elsematchs.[i]with|'A'..'Z'|'_'->false|_->aux(i+1)inaux0(* convert an arbitrary valid name to lowercase name which words are separated by
* dashes; for example "CamelCase" will become "camel-case"; already lowercased
* names will remain intact *)letnormalize_names=ifis_normal_namesthenselseU.string_of_list(normalize_list(U.list_of_strings))