Forum > Other

Split Name Quiz (Pascal Skill Challenge)

(1/5) > >>

x2nie:


Hi all,
Want do a brain sport?
Show your pascal programming skill here, it might be fun !


The task:
1) Create a function that do split component's name (a string) into several lines (another string separated with #13).
  eg: 'MainMenu1' ==> 'Main'#13'Menu1'.
2) Maximum width per line is 16 chars (100 pixels), with still meaningful per line, if possible.
  eg. OenPictureDialog2 ==> OpenPicture#13Dialog2, or Open#13PictureDialog2
3) Maximum lines count is 4 lines


Easy, isn't it?


I attached a Lazarus program that you can use as startup. But you can create your own program too.


---------------------------------
Story behind:
Why it is useful, or what benefit for Lazarus?
Well, it has been decades that any iconic component dropped in TDataModule that has long name,
and if there are many components, they seem crowded, hard to find, can't be categorized.
Now, having TDatamodule source-code (the Lazarus), we have a chance to make this situation
better.


The only problem is currently we have no function for splitting component name in good proportion.


If you only need the sample names, here is a list I used:

--- Code: ---
Memo1
MainMenu1
PopupMenu2
MenuItem3
TrayIcon4
Timer5
IdleTimer6
ApplicationProperties7
ImageList8
OpenDlg9
SaveDlg10
SelectDirectoryDlg11
ColorDlg12
FontDlg13
FindDlg14
ReplaceDlg15
TaskDlg16
OpenPictureDlg17
SavePictureDlg18
CalendarDlg19
CalculatorDlg20
Field21
StringField22
SmallintField23
LongintField24
WordField25
BooleanField26
FloatField27
CurrencyField28
BCDField29
DateField30
TimeField31
DateTimeField32
BytesField33
VarBytesField34
AutoIncField35
BlobField36
MemoField37
GraphicField38
WideStringField39
LargeintField40
VariantField41
GuidField42
FMTBCDField43
WideMemoField44
IntegerField45
ActionList46
LazCompQueue47
HTMLHelpDatabase48
HTMLBrowserHelpViewer49
PopupNotifier50
AsyncProcess51
XMLPropStorage52
IniPropStorage53
JSONPropStorage54
DataSource55
BufDataset56
CSVDataset57
SimpleIPCClient58
SimpleIPCServer59
XMLConfig60
EventLog61
Process62
SQLQuery63
SQLTransaction64
SQLScript65
SQLConnector66
MSSQLConn67
SybaseConn68
PQConn69
PQTEventMonitor70
OracleConn71
ODBCConn72
MySQL40Conn73
MySQL41Conn74
MySQL50Conn75
MySQL51Conn76
MySQL55Conn77
MySQL56Conn78
MySQL57Conn79
MySQL80Conn80
SQLite3Conn81
IBConn82
FBAdmin83
FBEventMonitor84
SQLDBLibraryLoader85
IDEDlgLayoutStorage86
ProcessUTF887
Carpet88
CarpetLabel89
CarpetImage90
LuiButton91
LuiGroupBox92
Button93
MyButton94
MyGroupBox95
Button96
GroupBox97
SynGutterPartList98
SynGutterSeparator99
SynGutterCodeFolding100
SynGutterLineNumber101
SynGutterChanges102
SynGutterMarks103
SynCompletion104
SynAutoComplete105
SynMacroRecorder106
SynExporterHTML107
SynPluginSyncroEdit108
SynPasSyn109
SynFreePascalSyn110
SynCppSyn111
SynJavaSyn112
SynJScriptSyn113
SynPerlSyn114
SynHTMLSyn115
SynXMLSyn116
SynLFMSyn117
SynDiffSyn118
SynUNIXShellScriptSyn119
SynCssSyn120
SynPHPSyn121
SynTeXSyn122
SynSQLSyn123
SynPythonSyn124
SynVBSyn125
SynAnySyn126
SynMultiSyn127
SynBatSyn128
SynIniSyn129
SynPoSyn130
Bitmap32List131
FPHTTPClient132
FPHttpServer133
HTMLDatasetContentProducer134
HTMLSelectProducer135
HTMLDatasetSelectProducer136
HTMLEntityProducer137
HTMLPageProducer138
HTMLDatasetFormShowProducer139
HTMLDatasetFormEditProducer140
HTMLDatasetFormGridProducer141
WebdataInputAdaptor142
FPWebDataProvider143
SQLDBWebDataProvider144
ExtJSJSonWebdataInputAdaptor145
ExtJSJSONDataFormatter146
ExtJSXMLWebdataInputAdaptor147
ExtJSXMLDataFormatter148
JSONRPCHandler149
JSONRPCDispatcher150
SessionJSONRPCDispatcher151
JSONRPCContentProducer152
ExtDirectDispatcher153
ExtDirectContentProducer154
OAuth2Handler155
FPOAuth2IniStore156

--- End code ---

x2nie:
my latest split name function is:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- function isUppercase(s:string): boolean;begin  result := uppercase(s) = s;end;  function isLowercase(s:string): boolean;begin  result := lowercase(s) = s;end;  function splitName(s:string):string;var ret : string; //resultvar i : integer;   function nextIsLowercase:boolean;   var c : string;   begin     c := copy(s, i+1,1);     result := (c <> EmptyStr) and isLowerCase(c);   end;  var  c : char;begin  ret := '';  for i := 1 to length(s) do  begin    c := s[i];    if (i > 1) and isUppercase(c) and nextIsLowercase() then       ret := ret + #13;    ret := ret + c;  end;  result := ret;end;     
come on, guys.
We can avoid crowded TDataModule by flipping the component names. Don't we?

x2nie:
well, I found that split with space is the best choice so far.
so the same line spaces need to be re-join soon

KodeZwerg:
I assume that I do fail (max 4 * "split") but anyway I like to show my approach:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program CustomSplit; {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$R *.res}{$ENDIF} // beautify a string by putting a space on logical positions// warning: limited to a-z and 0-9 (!)// written in a hurry by KodeZwerg 2022 for forum.lazarus.freepascal.orgfunction PrettyString(const AString: AnsiString; const Force: Boolean = False): AnsiString;  function IsUpper(const AChar: AnsiChar): Boolean; inline;  begin    Result := AChar in ['A'..'Z'];  end;  function IsLower(const AChar: AnsiChar): Boolean; inline;  begin    Result := AChar in ['a'..'z'];  end;  function IsNumber(const AChar: AnsiChar): Boolean; inline;  begin    Result := AChar in ['0'..'9'];  end;var  LCopy, LOutput: AnsiString;  LLen, i: Integer;begin  Result := AString;  // only work on strings that are longer than 16 chars, except you force it  if ((not Force) and (Length(AString) < 17)) then    Exit;  LCopy := AString;  LLen := Length(LCopy);  LOutput := '';  for i := 1 to LLen do    begin      if ( (Pred(i) > 0) and (Succ(i) <= LLen) and (IsLower(LCopy[Pred(i)])) and (IsUpper(LCopy[i])) ) then        LOutput := LOutput + ' ' + LCopy[i]      else      if ( (Pred(i) > 0) and (Succ(i) <= LLen) and (IsLower(LCopy[Pred(i)])) and (IsNumber(LCopy[i])) ) then        LOutput := LOutput + ' ' + LCopy[i]      else        LOutput := LOutput + LCopy[i];    end;  Result := LOutput;end; var  s: AnsiString;begin  s := 'HTMLDatasetFormGridProducer14';  WriteLn(s);  s := PrettyString(s, True);  WriteLn(s);  s := 'ExtJSJSonWebdataInputAdaptor14';  WriteLn(s);  s := PrettyString(s, True);  WriteLn(s);  {$IFDEF MSWINDOWS}  ReadLn;  {$ENDIF}end.Have a nice day!

//updated with y.ivanov good suggestion

y.ivanov:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    case AChar of      'a'..'z': Result := True;      else        Result := False;    end;
might be just simply:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Result := AChar in ['a'..'z'];

Navigation

[0] Message Index

[#] Next page

Go to full version