Verified Commit d3d1cf08 authored by rahulsainani's avatar rahulsainani Committed by Pier Angelo Vendrame
Browse files

Bug 1906024 - Format download file names better a=diannaS

parent a939bb5b
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ const val MAX_URI_LENGTH = 25000

private const val FILE_PREFIX = "file://"
private const val MAX_VALID_PORT = 65_535
private const val SPACE = " "
private const val UNDERSCORE = "_"

/**
 * Shortens URLs to be more user friendly.
@@ -307,7 +309,9 @@ fun String.sanitizeFileName(): String {
        file.name.replace("\\.\\.+".toRegex(), ".")
    } else {
        file.name.replace(".", "")
    }.replaceEscapedCharacters()
    }.replaceContinuousSpaces()
        .replaceEscapedCharacters()
        .trim()
}

/**
@@ -315,8 +319,16 @@ fun String.sanitizeFileName(): String {
 * and is correctly displayed.
 */
private fun String.replaceEscapedCharacters(): String {
    val controlCharactersRegex = "[\\x00-\\x13/*\"?<>:|\\\\]".toRegex()
    return replace(controlCharactersRegex, "_")
    val escapedCharactersRegex = "[\\x00-\\x13*\"?<>:|\\\\]".toRegex()
    return replace(escapedCharactersRegex, UNDERSCORE)
}

/**
 * Replaces continuous spaces with a single space.
 */
private fun String.replaceContinuousSpaces(): String {
    val escapedCharactersRegex = "\\s+".toRegex()
    return replace(escapedCharactersRegex, SPACE)
}

/**
+5 −5
Original line number Diff line number Diff line
@@ -199,11 +199,11 @@ class StringTest {
            "acknowledge\u0006signal" to "acknowledge_signal",
            "bell\u0007sound" to "bell_sound",
            "back\u0008space" to "back_space",
            "horizontal\u0009tab" to "horizontal_tab",
            "new\u000Aline" to "new_line",
            "vertical\u000Btab" to "vertical_tab",
            "form\u000Cfeed" to "form_feed",
            "return\u000Dcarriage" to "return_carriage",
            "horizontal\u0009tab" to "horizontal tab",
            "new\u000Aline" to "new line",
            "vertical\u000Btab" to "vertical tab",
            "form\u000Cfeed" to "form feed",
            "return\u000Dcarriage" to "return carriage",
            "shift\u000Eout" to "shift_out",
            "shift\u000Fin" to "shift_in",
            "escape\u0010data" to "escape_data",