Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 582861a

Browse files
committed
fixed bug in lastIndexOf
Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>
1 parent 2d7b48c commit 582861a

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/mediatype.gr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {lastIndexOf, reverse} from "./lib/stringutil"
66

77
export let default_mt = "application/octet-stream"
88

9+
// A giant map of all of the media types we know about.
910
let mut mediatypes = Map.make()
1011

1112
// Text formats
@@ -102,7 +103,12 @@ Map.set("woff", "font/woff", mediatypes)
102103
Map.set("woff2", "font/woff2", mediatypes)
103104

104105
// Guess the media type of this file
106+
//
107+
// Per recommendation, if no media type is found for an extension,
108+
// this returns `application/octet-stream`.
109+
//
105110
// @param filename: The name of the file
111+
// @returns String a media type
106112
export let guess = (filename: String) => {
107113
match (lastIndexOf(".", filename)) {
108114
Some(extOffset) => {

lib/stringutil.gr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import String from "string"
22
import Array from "array"
33

44
// Return a String that is the reverse of the given String.
5+
//
6+
// @param str: The string to reverse.
7+
// @return String A reversed version of the given string
58
export let reverse = (str: String) => {
69
let chars = String.explode(str)
710
let clen = Array.length(chars)
@@ -15,9 +18,11 @@ export let reverse = (str: String) => {
1518
// Get the index of the last appearance of needle in the haystack.
1619
// @param needle: The string to search for
1720
// @param haystack: The string to be searched
21+
// @return Option<Number> The offset, if found, or a number
1822
export let lastIndexOf = (needle: String, haystack: String) => {
1923
let rev = reverse(haystack)
20-
let i = String.indexOf(needle, rev)
24+
let revNeedle = reverse(needle)
25+
let i = String.indexOf(revNeedle, rev)
2126
match (i) {
2227
Some(offset) => Some(String.length(haystack) - 1 - offset),
2328
None => None,

tests.gr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let report = () => {
3535

3636
expect(("a", "b"), Env.splitEnvVar("a=b"), "Env.splitEnvVar should parse")
3737
expect("gfedcba", Util.reverse("abcdefg"), "Util.reverse should reverse string")
38-
expect(Some(5), Util.lastIndexOf("..", "aaaa.."), "UtillastIndexOf should find Some")
38+
expect(Some(5), Util.lastIndexOf("/.", "aaaa/."), "UtillastIndexOf should find Some")
3939
expect(None, Util.lastIndexOf("??", "aaaa.."), "Util.lastIndexOf should find None")
4040
expect("text/plain", Mediatype.guess("foo.txt"), "Mediatype.guess should find text/plain")
4141
expect("application/octet-stream", Mediatype.guess("foo.MADEUP"), "Mediatype.guess should find default type")

0 commit comments

Comments
 (0)