<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2014-03-12 13:35 GMT-03:00 Howard Page-Clark <span dir="ltr"><<a href="mailto:hdpc@talktalk.net" target="_blank">hdpc@talktalk.net</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
If you restrict yourself to Latin ansi encoding and 'number' means a numeric digit which does not have to be a whole word, the code is fairly simple since almost no parsing is required. Other cases and encodings require more complex string parsing. For example:</blockquote>
<div><br></div><div>It is not hard to allow chars like "ã", "Ã", "ñ", "Ñ", "ç", "Ç", "ä", "Ä" etc. I left the function a little faster too:</div>
<div><br></div><div>// old</div><div><div>function Valid(const s: string): boolean;</div><div><br></div><div> function HasLowerUpperNumeric: boolean;</div><div> var</div><div> c: char;</div><div> lower: boolean = False;</div>
<div> upper: boolean = False;</div><div> numeric: boolean = False;</div><div> begin</div><div> Result := False;</div><div> for c in s do</div><div> begin</div><div> if c in ['a'..'z'] then</div>
<div> lower := True</div><div> else if c in ['A'..'Z'] then</div><div> upper := True</div><div> else if c in ['0'..'9'] then</div><div> numeric := True;</div>
<div> if lower and upper and numeric then</div><div> Exit(True);</div><div> end;</div><div> end;</div><div><br></div><div>begin</div><div> if Length(s) < 3 then</div><div> Exit(False);</div><div> Result := HasLowerUpperNumeric;</div>
<div>end;</div><div><br></div><div>// new</div><div><div>function ValidPassword(const S: string): Boolean;</div><div>var</div><div> P: PChar;</div><div> F: Boolean;</div><div> L: Boolean = False;</div><div> U: Boolean = False;</div>
<div> N: Boolean = False;</div><div>begin</div><div> P := PChar(S);</div><div> while P^ <> #0 do</div><div> begin</div><div> case P^ of</div><div> #48..#57: N := True;</div><div> #65..#90: U := True;</div>
<div> #97..#122: L := True;</div><div> else</div><div> F := P^ = #195;</div><div> if F then</div><div> case P[1] of</div><div> #128..#159: U := True;</div><div> #160..#191: L := True;</div>
<div> end;</div><div> end;</div><div> Inc(P);</div><div> end;</div><div> Result := L and U and N;</div><div>end;</div></div><div><br></div><div>{ TForm1 }</div><div><br></div><div>procedure TForm1.Button1Click(Sender: TObject);</div>
<div>const</div><div> c = 20000000;</div><div>var</div><div> i: integer;</div><div> b, e: TDateTime;</div><div> s: string = 'Ãç1';</div><div> x: Boolean;</div><div>begin</div><div> // test Valid efficiency/performance</div>
<div> b := now;</div><div> for i := 0 to c do</div><div> begin</div><div> x := False;</div><div> x := Valid(s);</div><div> end;</div><div> e := now;</div><div> Edit1.Text := 'Valid: ' + BoolToStr(x, True) + ' - ' + formatdatetime('hh:nn:ss:zzz', b - e);</div>
<div><br></div><div> // test ValidPassword efficiency/performance<br></div><div> b := now;</div><div> for i := 0 to c do</div><div> begin</div><div> x := False;</div><div> x := ValidPassword(s);</div><div> end;</div>
<div> e := now;</div><div> Edit1.Text := Edit1.Text + ' / ValidPassword: ' + BoolToStr(x, True) + ' - ' + formatdatetime('hh:nn:ss:zzz', b - e);</div><div>end;</div></div><div><br></div><div>Result: Valid: <b>False</b> - 00:00:00:<b>861</b> / ValidPassword: <b>True</b> - 00:00:00:<b>559</b></div>
<div><br></div><div>Sure, it's a function that did just for explain, then it can be easily improved.<br></div><div><br></div></div>-- <br>Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a>
</div></div>