이 질문의 끝에서 Ada 문자열 처리에 도움을 요청하는 것 같습니다.
예, Ada 문자열은 실제로 크기를 조정할 수있는 버퍼가 아닌 정적 문자열로 처리하는 것이 가장 좋습니다. 이것을 처리 할 수있는 3 가지 typcial 방법이 있습니다.
첫 번째는 매우 큰 String
버퍼를 만들고, 문자열의 논리적 길이를 유지하는 변수 Natural
을 별도로 지정하는 것입니다. 이것은 다소 고통스럽고 다소 오류가 발생하기 쉽지만 적어도 버퍼의 끝에서 null을 지속적으로 검색하는 C의 방법보다 빠릅니다.
둘째는 그냥 펀트하여 Ada.Strings.Unbounded.Unbounded_String을 사용하는 것입니다. 절차적인 방법으로 사고하는 데 익숙하다면 이것은 가장 쉬운 방법이므로 대부분의 사람들이하는 일입니다.
세 번째는 가능하면 문자열을 처리하는 것입니다. 은입니다. 여기에 필요한 주요 통찰력은 Ada String
이 실제로 정적이지만 수명을 제어 할 수 있으며 기능적으로 프로그램하면 정적 문자열을 원할 때마다 동적으로 만들 수 있습니다.
function Matches_Token (Scanned : String) return boolean; --// Returns true if the given string is a token
function Could_Match_Longer (Scanned : String) return boolean; --// Returns true if the given string could be part of a larger token.
function Get_Next_Char return Character; --// Returns the next character from the stream
procedure Unget; --// Puts the last character back onto the stream
procedure Advance (Amount : Natural); --// Advance the stream pointer the given amount
function Longest_Matching_Token (Scanned : String) return String is
New_Token : constant String := Scanned & Get_Next_Char;
begin
--// Find the longest token a further scan can match
if Could_Match_Longer(New_Token) then
declare
LMT : constant String := Longest_Matching_Token (New_Token);
begin
if LMT /= "" then
unget;
return LMT;
end if;
end;
end if;
--// See if this string at least matches.
if Matches_Token(New_Token) then
unget;
return New_Token;
else
unget;
return "";
end if;
end Build_Token;
function Get_Next_Token return String is
Next_Token : constant String := Build_Token("");
begin
Advance (Next_Token'length);
return Next_Token;
end Get_Next_Token;
이 항상 문자열의 가장 효율적인 방법은 아닙니다 :
예를 들어, 나는 다음과 같은 일을 수행하여 내가 (이론적으로 무한 내다으로) 원하는 길이의 새로운 Token
문자열을 만들 수 있습니다 처리 (너무 많은 스택 사용),하지만 그것은 종종 가장 쉽습니다.
사실상, 스캐닝과 파싱은 실제로 버퍼 (방법 1)와 gotos와 같이 일반적으로 피해야하는 추악한 것들을 피하는 특별한 경우의 응용 프로그램입니다.
"sudo"또는 pseudo? – Coffee
IsAlNumOrUnderscore! –