Home / Function/ cleanPath() — gin Function Reference

cleanPath() — gin Function Reference

Architecture documentation for the cleanPath() function in path.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  e9bfd7f3_f139_ccbb_bda3_81e1d2a0636c["cleanPath()"]
  2cd24ad1_a2fb_ea62_1740_f464205e2482["path.go"]
  e9bfd7f3_f139_ccbb_bda3_81e1d2a0636c -->|defined in| 2cd24ad1_a2fb_ea62_1740_f464205e2482
  c9e13c4f_e8ac_23df_c197_fd4f4a014ce5["bufApp()"]
  e9bfd7f3_f139_ccbb_bda3_81e1d2a0636c -->|calls| c9e13c4f_e8ac_23df_c197_fd4f4a014ce5
  style e9bfd7f3_f139_ccbb_bda3_81e1d2a0636c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

path.go lines 23–124

func cleanPath(p string) string {
	// Turn empty string into "/"
	if p == "" {
		return "/"
	}

	// Reasonably sized buffer on stack to avoid allocations in the common case.
	// If a larger buffer is required, it gets allocated dynamically.
	buf := make([]byte, 0, stackBufSize)

	n := len(p)

	// Invariants:
	//      reading from path; r is index of next byte to process.
	//      writing to buf; w is index of next byte to write.

	// path must start with '/'
	r := 1
	w := 1

	if p[0] != '/' {
		r = 0

		if n+1 > stackBufSize {
			buf = make([]byte, n+1)
		} else {
			buf = buf[:n+1]
		}
		buf[0] = '/'
	}

	trailing := n > 1 && p[n-1] == '/'

	// A bit more clunky without a 'lazybuf' like the path package, but the loop
	// gets completely inlined (bufApp calls).
	// loop has no expensive function calls (except 1x make)		// So in contrast to the path package this loop has no expensive function
	// calls (except make, if needed).

	for r < n {
		switch {
		case p[r] == '/':
			// empty path element, trailing slash is added after the end
			r++

		case p[r] == '.' && r+1 == n:
			trailing = true
			r++

		case p[r] == '.' && p[r+1] == '/':
			// . element
			r += 2

		case p[r] == '.' && p[r+1] == '.' && (r+2 == n || p[r+2] == '/'):
			// .. element: remove to last /
			r += 3

			if w > 1 {
				// can backtrack
				w--

				if len(buf) == 0 {
					for w > 1 && p[w] != '/' {
						w--
					}
				} else {
					for w > 1 && buf[w] != '/' {
						w--
					}
				}
			}

		default:
			// Real path element.
			// Add slash if needed
			if w > 1 {
				bufApp(&buf, p, w, '/')
				w++
			}

			// Copy element
			for r < n && p[r] != '/' {

Domain

Subdomains

Defined In

Calls

Frequently Asked Questions

What does cleanPath() do?
cleanPath() is a function in the gin codebase, defined in path.go.
Where is cleanPath() defined?
cleanPath() is defined in path.go at line 23.
What does cleanPath() call?
cleanPath() calls 1 function(s): bufApp.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free