FileArgument.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2021, ry755 <ryanst755@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "FileArgument.h"
  8. #include <LibRegex/Regex.h>
  9. namespace TextEditor {
  10. FileArgument::FileArgument(String file_argument)
  11. {
  12. m_line = {};
  13. m_column = {};
  14. // A file doesn't exist with the full specified name, maybe the user entered
  15. // line/column coordinates?
  16. Regex<PosixExtended> re("^(.+?)(?::([0-9]+))?(?::([0-9]+))?$");
  17. RegexResult result = match(file_argument, re,
  18. PosixFlags::Global | PosixFlags::Multiline | PosixFlags::Ungreedy);
  19. auto& groups = result.capture_group_matches.at(0);
  20. // Match 0 group 0: file name
  21. // Match 0 group 1: line number
  22. // Match 0 group 2: column number
  23. if (groups.size() > 2) {
  24. // Both a line and column number were specified.
  25. auto filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors();
  26. auto initial_line_number = groups.at(1).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>();
  27. auto initial_column_number = groups.at(2).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>();
  28. m_filename = filename;
  29. if (initial_line_number.has_value() && initial_line_number.value() > 0)
  30. m_line = initial_line_number.value();
  31. if (initial_column_number.has_value())
  32. m_column = initial_column_number.value();
  33. } else if (groups.size() == 2) {
  34. // Only a line number was specified.
  35. auto filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors();
  36. auto initial_line_number = groups.at(1).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>();
  37. m_filename = filename;
  38. if (initial_line_number.has_value() && initial_line_number.value() > 0)
  39. m_line = initial_line_number.value();
  40. } else {
  41. // A colon was found at the end of the file name but no values were found
  42. // after it.
  43. m_filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors();
  44. }
  45. }
  46. }