Hone logo
Hone
Problems

Robust Path Manipulation in Go

This challenge focuses on implementing a set of functions to manipulate file paths in Go, mimicking some of the functionality found in the filepath package but with a focus on clarity and understanding. File path manipulation is a crucial skill for any Go developer, especially when dealing with file systems and I/O operations. Successfully completing this challenge will demonstrate your understanding of string manipulation and Go's standard library.

Problem Description

You are tasked with creating a module that provides functions for common path manipulation tasks. The module should include the following functions:

  1. Join(basePath string, pathSegments ...string) string: This function should join a base path with a variable number of path segments, correctly handling separators ( / on Unix-like systems, \ on Windows). It should normalize the resulting path by removing redundant separators and trailing separators.
  2. Clean(path string) string: This function should clean a path by removing redundant separators and resolving . and .. segments. . should be removed, and .. should move up one directory level (if possible). If .. attempts to move above the root directory, it should be ignored.
  3. IsAbsolute(path string) bool: This function should determine if a given path is absolute. An absolute path starts with / on Unix-like systems or a drive letter followed by a colon and backslash (e.g., C:\) on Windows.
  4. GetExtension(path string) string: This function should extract the file extension from a path. The extension is the portion of the filename after the last dot (.). If there is no dot or the path is a directory, return an empty string.

Key Requirements:

  • The functions should be robust and handle various edge cases correctly.
  • The functions should be platform-independent, meaning they should work correctly on both Unix-like and Windows systems. Assume the system's path separator is not known in advance and must be determined.
  • The functions should return an empty string when appropriate (e.g., no extension found).
  • Error handling is not required; invalid paths should be handled gracefully (e.g., by returning an empty string or the original path).

Expected Behavior:

  • Join should correctly combine paths, adding separators where needed.
  • Clean should resolve relative path components (. and ..).
  • IsAbsolute should accurately identify absolute paths.
  • GetExtension should extract the extension correctly.

Edge Cases to Consider:

  • Empty paths
  • Paths with leading/trailing separators
  • Paths with multiple consecutive separators
  • Paths with . and .. segments
  • Paths with no extension
  • Paths that are already absolute
  • Paths with spaces
  • Windows paths (drive letters, backslashes)

Examples

Example 1:

Input: Join("/home/user", "documents", "report.txt")
Output: /home/user/documents/report.txt
Explanation: Combines the base path with the segments, adding a separator where needed.

Example 2:

Input: Clean("/home/user/./documents/../report.txt")
Output: /home/user/report.txt
Explanation: Removes the `.` and resolves the `..`, simplifying the path.

Example 3:

Input: IsAbsolute("/home/user/report.txt")
Output: true
Explanation: The path starts with a `/`, indicating it's absolute.

Example 4:

Input: GetExtension("report.txt")
Output: txt
Explanation: Extracts the extension "txt" from the filename.

Example 5:

Input: GetExtension("mydirectory")
Output: ""
Explanation: No extension is present in the directory name.

Example 6:

Input: Join("path/to/dir", ".", "another/dir", "..")
Output: path/to/dir/another/dir
Explanation: Resolves the relative path components.

Constraints

  • All input paths will be strings.
  • The maximum length of a path string will be 1024 characters.
  • The functions should be reasonably efficient; avoid unnecessary string allocations.
  • The code should be well-documented and easy to understand.

Notes

  • Consider using strings.Builder for efficient string concatenation within the Join function.
  • Think carefully about how to handle different path separators across platforms. You can use os.PathSeparator to determine the appropriate separator.
  • The Clean function is the most complex; break it down into smaller, manageable steps.
  • Focus on correctness and clarity over extreme optimization. The goal is to demonstrate your understanding of path manipulation concepts.
  • Remember to handle edge cases gracefully.
Loading editor...
go