Solved

Quick String "Ticker"?

Posted on 2002-07-19
4
142 Views
Last Modified: 2010-04-04
Does anyone have any good ideas for a quick and easy routine, to "tick" through all combinations of a-z and 0-9 characters?

Rather like a stop-watch, but with characters.

Example :

aaaaaa
aaaaab
aaaaac
aaaaad
......
......
aaaaba
aaaabb
......
......
......
aaa9ca
aaa9cb
aaa9cc
......
etc. etc. etc.

Thanks in advance.
0
Comment
Question by:nicey
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 150 total points
ID: 7165760

An object oriented (and very fast) approach....

Rollover capability (when number is maxed "999..." etc) and works from right to left. Works by incrementing from a-z0-9, when 9 move left until not 9. If at null, then reset to all 'a', else increment and go left to right setting back to 'a'.
 
Hope this fits your needs,
Russell

---------------------------------

unit ticker;

interface

uses
  Windows, SysUtils;

type
  ETickClass     =  class(Exception);
  TTickClass     =  class(TObject)
  private
     // Private declaration
     FSize:      Integer;
     FBuffer:    PChar;
     FStart:     PChar;
     FHead:      PChar;
     FTail:      PChar;
  protected
     // Protected declaration
     function    GetValue: String;
  public
     // Public declaration
     constructor Create(Size: Integer);
     destructor  Destroy; override;
     property    Value: String read GetValue;
     property    Size: Integer read FSize;
     procedure   Tick;
  end;

implementation

constructor TTickClass.Create(Size: Integer);
begin

  // Perform inherited
  inherited Create;

  // Set values
  FBuffer:=nil;
  FSize:=Size;

  // Check size
  if (FSize <= 0) then raise ETickClass.Create('Size must be greater than zero');

  // Allocate buffer
  FBuffer:=AllocMem(Size+2);

  // Set up buffers and pointers
  FHead:=FBuffer+1;
  FillMemory(FHead, Size, Byte('a'));
  FTail:=StrEnd(FHead);
  FStart:=FTail;
  Dec(FStart);

end;

destructor TTickClass.Destroy;
begin

  // Free memory
  if Assigned(FBuffer) then FreeMem(FBuffer);

  // Perform inherited
  inherited Destroy;

end;

function TTickClass.GetValue;
begin

  // Return current buffer value
  result:=FHead;

end;

procedure TTickClass.Tick;
var  ppos:       PChar;
begin

  // Set starting position
  ppos:=FStart;

  // Handle current position
  case ppos^ of
     // Jump to numerics
     'z'   :  ppos^:='0';
     // Nine is where the real work comes in
     '9'   :
     begin
        // Loop all the way to the front while nines and not null
        while (ppos^ = '9') do Dec(ppos);
        // If the whole string is nines then reset all to a's and were done
        if (ppos^ = #0) then
           FillMemory(FHead, FSize, Byte('a'))
        else
        begin
           // We are not at null, so increment byte
           if (ppos^ = 'z') then
              ppos^:='0'
           else
              Inc(Byte(ppos^));
           // Walk back from left to right
           Inc(ppos);
           while (ppos^ <> #0) do
           begin
              ppos^:='a';
              Inc(ppos);
           end;
        end;
     end;
  else
     Inc(Byte(ppos^));
  end;

end;

0
 
LVL 1

Author Comment

by:nicey
ID: 7165825
Mate, you're a star!

It works very nice indeed.  I've hooked it into my program, and the job is now a good one.

I'm just off to finish off the project.

Thanks again. ;-)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 7165852

Anytime :-)

I also should have mentioned that the Size param on the create is used to define the length of your ticker string, but I think you already found that out.

Have a great weekend,
Russell
0
 
LVL 1

Author Comment

by:nicey
ID: 7165896
Yeah, the code is pretty tight - it only took a few read throughs, and it all sunk in.

Only thing is, my 500Mhz AMD chip is really "running" when working with the larger patterns. :-D

S'ok though, as the final program will be run a much much more upto date system.

Good weekend to you too. ;-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
QRReport  TQrmemo vertical stretching 1 53
Delphi 2 69
Working with hours 3 65
TServerSocket - file via sendbuf or Text via sendtext ? 1 66
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question