match.odin 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package filepath
  2. import "core:os"
  3. import "core:slice"
  4. import "core:strings"
  5. import "core:unicode/utf8"
  6. Match_Error :: enum {
  7. None,
  8. Syntax_Error,
  9. }
  10. // match states whether "name" matches the shell pattern
  11. // Pattern syntax is:
  12. // pattern:
  13. // {term}
  14. // term:
  15. // '*' matches any sequence of non-/ characters
  16. // '?' matches any single non-/ character
  17. // '[' ['^'] { character-range } ']'
  18. // character classification (cannot be empty)
  19. // c matches character c (c != '*', '?', '\\', '[')
  20. // '\\' c matches character c
  21. //
  22. // character-range
  23. // c matches character c (c != '\\', '-', ']')
  24. // '\\' c matches character c
  25. // lo '-' hi matches character c for lo <= c <= hi
  26. //
  27. // match requires that the pattern matches the entirety of the name, not just a substring
  28. // The only possible error returned is .Syntax_Error
  29. //
  30. // NOTE(bill): This is effectively the shell pattern matching system found
  31. //
  32. match :: proc(pattern, name: string) -> (matched: bool, err: Match_Error) {
  33. pattern, name := pattern, name
  34. pattern_loop: for len(pattern) > 0 {
  35. star: bool
  36. chunk: string
  37. star, chunk, pattern = scan_chunk(pattern)
  38. if star && chunk == "" {
  39. return !strings.contains(name, SEPARATOR_STRING), .None
  40. }
  41. t: string
  42. ok: bool
  43. t, ok, err = match_chunk(chunk, name)
  44. if ok && (len(t) == 0 || len(pattern) > 0) {
  45. name = t
  46. continue
  47. }
  48. if err != .None {
  49. return
  50. }
  51. if star {
  52. for i := 0; i < len(name) && name[i] != SEPARATOR; i += 1 {
  53. t, ok, err = match_chunk(chunk, name[i+1:])
  54. if ok {
  55. if len(pattern) == 0 && len(t) > 0 {
  56. continue
  57. }
  58. name = t
  59. continue pattern_loop
  60. }
  61. if err != .None {
  62. return
  63. }
  64. }
  65. }
  66. return false, .None
  67. }
  68. return len(name) == 0, .None
  69. }
  70. @(private="file")
  71. scan_chunk :: proc(pattern: string) -> (star: bool, chunk, rest: string) {
  72. pattern := pattern
  73. for len(pattern) > 0 && pattern[0] == '*' {
  74. pattern = pattern[1:]
  75. star = true
  76. }
  77. in_range, i := false, 0
  78. scan_loop: for i = 0; i < len(pattern); i += 1 {
  79. switch pattern[i] {
  80. case '\\':
  81. when ODIN_OS != "windows" {
  82. if i+1 < len(pattern) {
  83. i += 1
  84. }
  85. }
  86. case '[':
  87. in_range = true
  88. case ']':
  89. in_range = false
  90. case '*':
  91. if !in_range {
  92. break scan_loop
  93. }
  94. }
  95. }
  96. return star, pattern[:i], pattern[i:]
  97. }
  98. @(private="file")
  99. match_chunk :: proc(chunk, s: string) -> (rest: string, ok: bool, err: Match_Error) {
  100. chunk, s := chunk, s
  101. for len(chunk) > 0 {
  102. if len(s) == 0 {
  103. return
  104. }
  105. switch chunk[0] {
  106. case '[':
  107. r, w := utf8.decode_rune_in_string(s)
  108. s = s[w:]
  109. chunk = chunk[1:]
  110. is_negated := false
  111. if len(chunk) > 0 && chunk[0] == '^' {
  112. is_negated = true
  113. chunk = chunk[1:]
  114. }
  115. match := false
  116. range_count := 0
  117. for {
  118. if len(chunk) > 0 && chunk[0] == ']' && range_count > 0 {
  119. chunk = chunk[1:]
  120. break
  121. }
  122. lo, hi: rune
  123. if lo, chunk, err = get_escape(chunk); err != .None {
  124. return
  125. }
  126. hi = lo
  127. if chunk[0] == '-' {
  128. if hi, chunk, err = get_escape(chunk[1:]); err != .None {
  129. return
  130. }
  131. }
  132. if lo <= r && r <= hi {
  133. match = true
  134. }
  135. range_count += 1
  136. }
  137. if match == is_negated {
  138. return
  139. }
  140. case '?':
  141. if s[0] == SEPARATOR {
  142. return
  143. }
  144. _, w := utf8.decode_rune_in_string(s)
  145. s = s[w:]
  146. chunk = chunk[1:]
  147. case '\\':
  148. when ODIN_OS != "windows" {
  149. chunk = chunk[1:]
  150. if len(chunk) == 0 {
  151. err = .Syntax_Error
  152. return
  153. }
  154. }
  155. fallthrough
  156. case:
  157. if chunk[0] != s[0] {
  158. return
  159. }
  160. s = s[1:]
  161. chunk = chunk[1:]
  162. }
  163. }
  164. return s, true, .None
  165. }
  166. @(private="file")
  167. get_escape :: proc(chunk: string) -> (r: rune, next_chunk: string, err: Match_Error) {
  168. if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' {
  169. err = .Syntax_Error
  170. return
  171. }
  172. chunk := chunk
  173. if chunk[0] == '\\' && ODIN_OS != "windows" {
  174. chunk = chunk[1:]
  175. if len(chunk) == 0 {
  176. err = .Syntax_Error
  177. return
  178. }
  179. }
  180. w: int
  181. r, w = utf8.decode_rune_in_string(chunk)
  182. if r == utf8.RUNE_ERROR && w == 1 {
  183. err = .Syntax_Error
  184. }
  185. next_chunk = chunk[w:]
  186. if len(next_chunk) == 0 {
  187. err = .Syntax_Error
  188. }
  189. return
  190. }
  191. // glob returns the names of all files matching pattern or nil if there are no matching files
  192. // The syntax of patterns is the same as "match".
  193. // The pattern may describe hierarchical names such as /usr/*/bin (assuming '/' is a separator)
  194. //
  195. // glob ignores file system errors
  196. //
  197. glob :: proc(pattern: string, allocator := context.allocator) -> (matches: []string, err: Match_Error) {
  198. if !has_meta(pattern) {
  199. // TODO(bill): os.lstat on here to check for error
  200. m := make([]string, 1, allocator)
  201. m[0] = pattern
  202. return m[:], .None
  203. }
  204. temp_buf: [8]byte
  205. dir, file := split(pattern)
  206. volume_len := 0
  207. when ODIN_OS == "windows" {
  208. volume_len, dir = clean_glob_path_windows(dir, temp_buf[:])
  209. } else {
  210. dir = clean_glob_path(dir)
  211. }
  212. if !has_meta(dir[volume_len:]) {
  213. m, e := _glob(dir, file, nil)
  214. return m[:], e
  215. }
  216. m: []string
  217. m, err = glob(dir)
  218. if err != .None {
  219. return
  220. }
  221. dmatches := make([dynamic]string, 0, 0, allocator)
  222. for d in m {
  223. dmatches, err = _glob(d, file, &dmatches)
  224. if err != .None {
  225. break
  226. }
  227. }
  228. if len(dmatches) > 0 {
  229. matches = dmatches[:]
  230. }
  231. return
  232. }
  233. _glob :: proc(dir, pattern: string, matches: ^[dynamic]string) -> (m: [dynamic]string, e: Match_Error) {
  234. if matches != nil {
  235. m = matches^
  236. } else {
  237. m = make([dynamic]string, 0, 0, context.allocator)
  238. }
  239. d, derr := os.open(dir)
  240. if derr != 0 {
  241. return
  242. }
  243. defer os.close(d)
  244. {
  245. file_info, ferr := os.fstat(d)
  246. defer os.file_info_delete(file_info)
  247. if ferr != 0 {
  248. return
  249. }
  250. if !file_info.is_dir {
  251. return
  252. }
  253. }
  254. fis, _ := os.read_dir(d, -1)
  255. slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
  256. return a.name < b.name
  257. })
  258. defer {
  259. for fi in fis {
  260. os.file_info_delete(fi)
  261. }
  262. delete(fis)
  263. }
  264. for fi in fis {
  265. n := fi.name
  266. matched := match(pattern, n) or_return
  267. if matched {
  268. append(&m, join(dir, n))
  269. }
  270. }
  271. return
  272. }
  273. @(private)
  274. has_meta :: proc(path: string) -> bool {
  275. when ODIN_OS == "windows" {
  276. CHARS :: `*?[`
  277. } else {
  278. CHARS :: `*?[\`
  279. }
  280. return strings.contains_any(path, CHARS)
  281. }
  282. @(private)
  283. clean_glob_path :: proc(path: string) -> string {
  284. switch path {
  285. case "":
  286. return "."
  287. case SEPARATOR_STRING:
  288. return path
  289. }
  290. return path[:len(path)-1]
  291. }
  292. @(private)
  293. clean_glob_path_windows :: proc(path: string, temp_buf: []byte) -> (prefix_len: int, cleaned: string) {
  294. vol_len := volume_name_len(path)
  295. switch {
  296. case path == "":
  297. return 0, "."
  298. case vol_len+1 == len(path) && is_separator(path[len(path)-1]): // /, \, C:\, C:/
  299. return vol_len+1, path
  300. case vol_len == len(path) && len(path) == 2: // C:
  301. copy(temp_buf[:], path)
  302. temp_buf[2] = '.'
  303. return vol_len, string(temp_buf[:3])
  304. }
  305. if vol_len >= len(path) {
  306. vol_len = len(path) -1
  307. }
  308. return vol_len, path[:len(path)-1]
  309. }