nobody at rubyforge.org
2007-Apr-03 18:46 UTC
[Wxruby-development] [943] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Fix bug with false positives from #find_string, bounds-checking for
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:'':'';} #msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[943] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Fix bug with false positives from #find_string, bounds-checking for</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>943</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2007-04-03 14:46:45 -0400 (Tue, 03 Apr 2007)</dd> </dl> <h3>Log Message</h3> <pre>Fix bug with false positives from #find_string, bounds-checking for proxy collections</pre> <h3>Modified Paths</h3> <ul> <li><a href="#trunkwxsugarlibwx_sugarenumerable_controlsrb">trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="trunkwxsugarlibwx_sugarenumerable_controlsrb"></a> <div class="modfile"><h4>Modified: trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb (942 => 943)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb        2007-04-03 18:45:26 UTC (rev 942) +++ trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb        2007-04-03 18:46:45 UTC (rev 943) </span><span class="lines">@@ -51,11 +51,12 @@ </span><span class="cx"> # +value+. +value+ may be a string label, or it may be any ruby object </span><span class="cx"> # set as client data to a list item. Returns +nil+ if nothing matches. </span><span class="cx"> def index(value) </span><del>- if value.kind_of?(String) and i = find_string(value) </del><ins>+ # -1 means ''not found'' in WxWidgets + if value.kind_of?(String) && ( i = find_string(value, true) ) && i > -1 </ins><span class="cx"> return i </span><span class="cx"> end </span><span class="cx"> indices = (0 ... get_count).to_a </span><del>- return indices.find { | i | get_item_data(i) == value } </del><ins>+ return indices.find { | i | get_item_data(i) == value } </ins><span class="cx"> end </span><span class="cx"> </span><span class="cx"> # Appends this string to the control </span><span class="lines">@@ -72,11 +73,35 @@ </span><span class="cx"> end </span><span class="cx"> </span><span class="cx"> # These classes provide proxy accessors for items within the control. </span><ins>+ # ItemCollection is an abstract base class. </ins><span class="cx"> class ItemCollection </span><span class="cx"> attr_reader :cwi </span><span class="cx"> def initialize(cwi) </span><span class="cx"> @cwi = cwi </span><span class="cx"> end </span><ins>+ + # Do a bounds-checked call of some value-fetching method +methdo+ + # for the item +i+ + def [](method, i) + unless index?(i) + raise IndexError, "No such item #{i} in #{self.cwi}" + end + cwi.send( method, i ) + end + + # Do a bounds-checked call of some value-setting method +methdo+ + # for the item +i+ to value +val+ + def []=(method, i, val) + unless index?(i) + raise IndexError, "No such item #{i} in #{self.cwi}" + end + cwi.send( method, i, val ) + end + + # over-ridden by some subclasses + def index?(i) + i >= 0 and i < cwi.get_count + end </ins><span class="cx"> end </span><span class="cx"> </span><span class="cx"> # A proxy class for the item_data functions. </span><span class="lines">@@ -85,12 +110,14 @@ </span><span class="cx"> cwi.each { | i | yield cwi.get_item_data(i) } </span><span class="cx"> end </span><span class="cx"> </span><ins>+ # Retrieves the item data for item +i+ </ins><span class="cx"> def [](i) </span><del>- cwi.get_item_data(i) </del><ins>+ super :get_item_data, i </ins><span class="cx"> end </span><span class="cx"> </span><ins>+ # Sets the item data for +i+ to be +obj+ </ins><span class="cx"> def []=(i, obj) </span><del>- cwi.set_item_data(i, obj) </del><ins>+ super :set_item_data, i, obj </ins><span class="cx"> end </span><span class="cx"> end </span><span class="cx"> </span><span class="lines">@@ -101,12 +128,35 @@ </span><span class="cx"> cwi.each { | i | yield cwi.get_string(i) } </span><span class="cx"> end </span><span class="cx"> </span><ins>+ # Retrieves the string for item +i+ </ins><span class="cx"> def [](i) </span><del>- cwi.get_string(i) </del><ins>+ super :get_string, i </ins><span class="cx"> end </span><span class="cx"> </span><ins>+ # Sets a string within the control </ins><span class="cx"> def []=(i, str) </span><del>- cwi.set_string(i, str) </del><ins>+ super :set_string, i, str </ins><span class="cx"> end </span><span class="cx"> end </span><ins>+ + # Fetching strings from a ListCtrl + class ListItemTextCollection < ItemCollection + def each + cwi.each { | i | yield cwi.get_item_text(i) } + end + + # Retrieves the string for item +i+ + def [](i) + super :get_item_text, i + end + + # Sets a string within the control + def []=(i, str) + super :set_item_text, i, str + end + + def index?(i) + i >= 0 and i < cwi.get_item_count + end + end </ins><span class="cx"> end </span></span></pre> </div> </div> </body> </html>
Apparently Analagous Threads
- [937] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Initial commit of enumerable_controls.rb
- [940] trunk/wxsugar/lib/wx_sugar/wx_classes/treectrl.rb: Align #traverse usage with #each in enumerable_controls.rb
- Bug? in wxSugar enumerable_controls.rb
- [944] trunk/wxsugar/lib/wx_sugar/wx_classes/listctrl.rb: Fix bug with endless recursion on not-found items in find_string
- [938] trunk/wxsugar/lib/wx_sugar/wx_classes: Enable enumerable_controls for ListCtrl and ControlWithItems family