Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a StrToResID function #5

Open
delphidabbler opened this issue Nov 20, 2022 · 1 comment
Open

Implement a StrToResID function #5

delphidabbler opened this issue Nov 20, 2022 · 1 comment
Assignees
Labels
considering enhancement New feature or request

Comments

@delphidabbler
Copy link
Member

There's already a ResIDToStr function. It's inverse would be useful if, when presented with a suitably formatted string it could be converted back to a resource ID.

Parsing rules:

  • If string starts with # and rest is numeric, then the resource ID is numeric and can be created with MakeIntResource.
  • Otherwise resource ID is just a Pointer to the string itself, which should be OK providing the string passed as parameter remains in scope.
@delphidabbler delphidabbler self-assigned this Nov 20, 2022
@delphidabbler delphidabbler added enhancement New feature or request considering labels Nov 20, 2022
@delphidabbler
Copy link
Member Author

Possible code, untested:

function IsValidResIDStr(Str: string): Boolean;
begin
  Result := False;
  if Length(Str) = 0 then
    Exit;
  if Str[1] = '#' then
  begin
    if Length(Str) = 1 then
      Exit;
    for var I := 2 to Length(Str) do
    begin
      if not TCharacter.IsNumeric(Str[I]) then
        Exit;
    end;
    Result := True;
  end
  else
    Result := True;
end;

function StrToResID(Str: string): PChar;
begin
  if not IsValidResIDStr(Str) then
    raise Exception.Create('...');
  if Str[1] = '#' then
  begin
    var NStr := RightStr(Str, Length(Str) - 1);
    var N := StrToInt(NStr);
    Result := MakeIntResource(N);
  end
  else
    Result := PChar(Str);
end;

Notice how the code is much clearer with function IsValidResIDStr defined, without too much code being run twice. We might as well make that routine public too.

@delphidabbler delphidabbler changed the title Implemented a StrToResID function Implement a StrToResID function Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
considering enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant