Module: Datadog::DI::Utils Private

Defined in:
lib/datadog/di/utils.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.path_matches_suffix?(path, suffix) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the provided path matches the user-designated file suffix (of a line probe).

If suffix is an absolute path (i.e., it starts with a slash), the path must be identical for it to match.

If suffix is not an absolute path, the path matches if its suffix is the provided suffix, at a path component boundary.

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/datadog/di/utils.rb', line 14

module_function def path_matches_suffix?(path, suffix)
  if path.nil?
    raise ArgumentError, "nil path passed"
  end
  if suffix.nil?
    raise ArgumentError, "nil suffix passed"
  end

  if suffix.start_with?('/')
    path == suffix
  else
    # Exact match is not possible here, meaning any matching path
    # has to be longer than the suffix. Require full component matches,
    # meaning either the first character of the suffix is a slash
    # or the previous character in the path is a slash.
    # For now only check for forward slashes for Unix-like OSes;
    # backslash is a legitimate character of a file name in Unix
    # therefore simply permitting forward or back slash is not
    # sufficient, we need to perform an OS check to know which
    # path separator to use.
    !!
    if path.length > suffix.length && path.end_with?(suffix)
      previous_char = path[path.length - suffix.length - 1]
      previous_char == "/" || suffix[0] == "/"
    end

    # Alternative implementation using a regular expression:
    # !!(path =~ %r,(/|\A)#{Regexp.quote(suffix)}\z,)
  end
end