Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-11

EnumToString

9 messages · 8 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen

Hi

I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new.

How is the syntax?

Juha

Reply by email

Yann Bat
to Juha Manninen: Hi I remember there is a way to get a string representation of an enumerated type…

You can directly write enums with Write or WriteLn and convert enums to strings with WriteStr.

program EnumStr;
{$mode objfpc}{$H+}
type
  TColor = (cRed, cGreen, cBlue, cYellow);
function ColorToStr(C: TColor) : String;
begin
  WriteStr(Result, C);
end;
var
  Color : TColor;
begin
  for Color in TColor do
    WriteLn(ColorToStr(Color));
end.

2011/11/1 Juha Manninen :

Show quoted text 11 lines
Hi I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new. How is the syntax? Juha _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Tomas Hajny
to Juha Manninen: Hi I remember there is a way to get a string representation of an enumerated type…

On Tue, November 1, 2011 08:41, Juha Manninen wrote:

Hi,

Show quoted text 6 lines
I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new. How is the syntax?
type
 TNum = (One, Two, Three);
var
 N: TNum;
 S: string;
begin
 N := One;
 WriteLn (N);
 N := Two;
 WriteStr (S, N);
 WriteLn (S);
end.

Tomas

Reply by email

Thomas Young
to Juha Manninen: Hi I remember there is a way to get a string representation of an enumerated type…

Hi,

This is code I wrote for converting 1, 2 or 3 digit numbers to string.

	function NumtoString(N:integer):string;
	var
		c1, c2, c3: char;
	begin
		if (N<10) then
			NumtoString := chr(N+48)
		else if (N > 9) and (N < 100) then
			begin
				c1 := chr((N div 10) + 48);
				c2 := chr((N mod 10) + 48);
				NumtoString := concat(c1, c2);
			end
		else if (N > 99) and (N < 1000) then
			begin
				c1 := chr((N div 100) + 48);
				c2 := chr((N mod 100) div 10 + 48);
				c3 := chr((N mod 10) + 48);
				NumtoString := concat(c1, c2, c3);
			end;
	end;

Thomas Young
cell: 330-256-7064
mobile email:

Juha Manninen wrote 12 lines
Hi I remember there is a way to get a string representation of an enumerated type directly without using a lookup string array, but I forgot the syntax. Lazarus uses only lookup arrays, maybe because the other syntax is new. How is the syntax? Juha _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Juha Manninen
to Tomas Hajny: type TNum = (One, Two, Three); var N: TNum; S: string; begin N := One; WriteLn (N); N :=…
WriteStr (S, N);

Thanks guys!
WriteStr() was the function I was looking for.
I found some places in Lazarus code where it is used but not many. Martin's debugger code has some.
I think many lookup string arrays in Lazarus code could be replaces with WriteStr. It would reduce code size and improve code maintenance.

If I understand right WriteStr uses RTTI internally. So, every enum type already carries the string explicit lookup
array is duplicate data.

Juha

Reply by email

Vincent Snijders
to Juha Manninen

2011/11/1 Juha Manninen :

Show quoted text 11 lines
>  WriteStr (S, N); Thanks guys! WriteStr() was the function I was looking for. I found some places in Lazarus code where it is used but not many. Martin's debugger code has some. I think many lookup string arrays in Lazarus code could be replaces with WriteStr. It would reduce code size and improve code maintenance. If I understand right WriteStr uses RTTI internally. So, every enum type already carries the string explicit lookup array is duplicate data.

It is a relatively new feature, since fpc 2.4.0 IIRC.

Vincent

Reply by email

Florian Klämpfl
to Juha Manninen: Thanks guys! WriteStr() was the function I was looking for. I found some places in…
Juha Manninen wrote 13 lines
WriteStr (S, N); Thanks guys! WriteStr() was the function I was looking for. I found some places in Lazarus code where it is used but not many. Martin's debugger code has some. I think many lookup string arrays in Lazarus code could be replaces with WriteStr. It would reduce code size and improve code maintenance. If I understand right WriteStr uses RTTI internally. So, every enum type already carries the string explicit lookup array is duplicate data.

Not really, if rtti for a certain enum is never used, the linker won't include it.

Reply by email