Solved

Quick String "Ticker"?

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

13 Experts available now in Live!

Get 1:1 Help Now