Solved

Quick String "Ticker"?

Posted on 2002-07-19
4
140 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory leak using records in a TVirtualStringTree 11 109
Delphi cmd execution 6 56
Intraweb download file link ? 1 109
Dynamically Created Query 3 50
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now