Skip to content

Commit

Permalink
fixed bug in lastIndexOf
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Butcher <matt.butcher@microsoft.com>
  • Loading branch information
technosophos committed Jun 4, 2021
1 parent 2d7b48c commit 582861a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/mediatype.gr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {lastIndexOf, reverse} from "./lib/stringutil"

export let default_mt = "application/octet-stream"

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

// Text formats
Expand Down Expand Up @@ -102,7 +103,12 @@ Map.set("woff", "font/woff", mediatypes)
Map.set("woff2", "font/woff2", mediatypes)

// Guess the media type of this file
//
// Per recommendation, if no media type is found for an extension,
// this returns `application/octet-stream`.
//
// @param filename: The name of the file
// @returns String a media type
export let guess = (filename: String) => {
match (lastIndexOf(".", filename)) {
Some(extOffset) => {
Expand Down
7 changes: 6 additions & 1 deletion lib/stringutil.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import String from "string"
import Array from "array"

// Return a String that is the reverse of the given String.
//
// @param str: The string to reverse.
// @return String A reversed version of the given string
export let reverse = (str: String) => {
let chars = String.explode(str)
let clen = Array.length(chars)
Expand All @@ -15,9 +18,11 @@ export let reverse = (str: String) => {
// Get the index of the last appearance of needle in the haystack.
// @param needle: The string to search for
// @param haystack: The string to be searched
// @return Option<Number> The offset, if found, or a number
export let lastIndexOf = (needle: String, haystack: String) => {
let rev = reverse(haystack)
let i = String.indexOf(needle, rev)
let revNeedle = reverse(needle)
let i = String.indexOf(revNeedle, rev)
match (i) {
Some(offset) => Some(String.length(haystack) - 1 - offset),
None => None,
Expand Down
2 changes: 1 addition & 1 deletion tests.gr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let report = () => {

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

0 comments on commit 582861a

Please sign in to comment.