Serenity Operating System
at master 53 lines 2.2 kB view raw
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 8#include "FileArgument.h" 9#include <LibRegex/Regex.h> 10 11namespace TextEditor { 12 13FileArgument::FileArgument(String file_argument) 14{ 15 m_line = {}; 16 m_column = {}; 17 18 // A file doesn't exist with the full specified name, maybe the user entered 19 // line/column coordinates? 20 Regex<PosixExtended> re("^(.+?)(?::([0-9]+))?(?::([0-9]+))?$"); 21 RegexResult result = match(file_argument, re, 22 PosixFlags::Global | PosixFlags::Multiline | PosixFlags::Ungreedy); 23 auto& groups = result.capture_group_matches.at(0); 24 25 // Match 0 group 0: file name 26 // Match 0 group 1: line number 27 // Match 0 group 2: column number 28 if (groups.size() > 2) { 29 // Both a line and column number were specified. 30 auto filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors(); 31 auto initial_line_number = groups.at(1).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>(); 32 auto initial_column_number = groups.at(2).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>(); 33 34 m_filename = filename; 35 if (initial_line_number.has_value() && initial_line_number.value() > 0) 36 m_line = initial_line_number.value(); 37 if (initial_column_number.has_value()) 38 m_column = initial_column_number.value(); 39 } else if (groups.size() == 2) { 40 // Only a line number was specified. 41 auto filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors(); 42 auto initial_line_number = groups.at(1).view.to_string().release_value_but_fixme_should_propagate_errors().to_number<int>(); 43 44 m_filename = filename; 45 if (initial_line_number.has_value() && initial_line_number.value() > 0) 46 m_line = initial_line_number.value(); 47 } else { 48 // A colon was found at the end of the file name but no values were found 49 // after it. 50 m_filename = groups.at(0).view.to_string().release_value_but_fixme_should_propagate_errors(); 51 } 52} 53}