Permalink
Browse files

Store the buffer size value in a class variable of the Hpricot module…

… (@@buffer_size).
  • Loading branch information...
1 parent 5829ca8 commit d5bec9c777029a447b03e3b1a38b931f8e05b1bd @nrk committed Jul 17, 2010
Showing with 24 additions and 18 deletions.
  1. +7 −7 src/Hpricot.cs
  2. +17 −11 src/HpricotScanner.cs
View
14 src/Hpricot.cs
@@ -29,14 +29,11 @@ public static class Hpricot {
public static Object Scan(ConversionStorage<MutableString>/*!*/ toMutableStringStorage, RespondToStorage/*!*/ respondsTo,
BinaryOpStorage/*!*/ readIOStorage, BlockParam block, RubyModule/*!*/ self, Object/*!*/ source, Hash/*!*/ options) {
- // TODO: improve me please!
Object elementContent;
if (!self.TryGetConstant(null, "ElementContent", out elementContent) && !(elementContent is Hash)) {
throw new Exception("Hpricot::ElementContent is missing or it is not an Hash");
}
-
- //NOTE: block can be null as of Hpricot 0.7, see HpricotScanner.ELE
- HpricotScanner scanner = new HpricotScanner(toMutableStringStorage, readIOStorage, block);
+ var scanner = new HpricotScanner(toMutableStringStorage, readIOStorage, block);
return scanner.Scan(source, options, elementContent as Hash);
}
@@ -47,13 +44,16 @@ public static class Hpricot {
[RubyMethod("buffer_size", RubyMethodAttributes.PublicSingleton)]
public static Int32? GetBufferSize(RubyModule/*!*/ self) {
- return HpricotScanner.BufferSize;
+ Object bufferSize;
+ if (self.TryGetClassVariable("@@buffer_size", out bufferSize)) {
+ return (int) bufferSize;
+ }
+ return null;
}
[RubyMethod("buffer_size=", RubyMethodAttributes.PublicSingleton)]
public static void SetBufferSize(RubyModule/*!*/ self, Int32 bufferSize) {
- // TODO: thread safety
- HpricotScanner.BufferSize = bufferSize;
+ self.SetClassVariable("@@buffer_size", bufferSize);
}
#endregion
View
28 src/HpricotScanner.cs
@@ -17,8 +17,6 @@ public class HpricotScanner {
#region fields - miscellaneous
private static readonly String NO_WAY_SERIOUSLY = "*** This should not happen, please send a bug report with the HTML you're parsing to why@whytheluckystiff.net. So sorry!";
-
- private static Int32? _bufferSize;
private static readonly RubyRegex _procInsParse = new RubyRegex(MutableString.CreateAscii(@"\A<\?(\S+)\s+(.+)"), RubyRegexOptions.Multiline);
private RubyContext/*!*/ _context;
@@ -66,7 +64,7 @@ public class HpricotScanner {
Object[] tag, akey, aval;
int mark_tag, mark_akey, mark_aval;
bool done = false, ele_open = false;
- int buffer_size = 0;
+ int buffer_size = DEFAULT_BUFFER_SIZE;
bool taint = false;
#endregion
@@ -1010,12 +1008,7 @@ public class HpricotScanner {
#region static properties
-
- public static Int32? BufferSize {
- get { return _bufferSize; }
- set { _bufferSize = value; }
- }
-
+
public static RubyRegex ProcInsParse {
get { return _procInsParse; }
}
@@ -1101,6 +1094,20 @@ public class HpricotScanner {
he2.Parent = focus;
}
+ private static int GetBufferSize(RubyContext context) {
+ RubyModule hpricotModule;
+ if (!context.TryGetModule(typeof(Hpricot), out hpricotModule)) {
+ RubyExceptions.CreateNameError("Cannot find module Hpricot");
+ }
+
+ Object bufferSize;
+ if (hpricotModule.TryGetClassVariable("@@buffer_size", out bufferSize)) {
+ return (int)bufferSize;
+ }
+
+ return DEFAULT_BUFFER_SIZE;
+ }
+
#endregion
@@ -1273,7 +1280,6 @@ public class HpricotScanner {
}
else if (sym_cdata.Equals(sym)) {
-
rb_hpricot_add(state.Focus, H_ELE(new Hpricot.CData(state), state, sym, tag, attr, ec, raw, rawlen));
}
else if (sym_comment.Equals(sym)) {
@@ -1505,7 +1511,7 @@ public class HpricotScanner {
_state = state;
}
- buffer_size = BufferSize.HasValue ? BufferSize.Value : DEFAULT_BUFFER_SIZE;
+ buffer_size = GetBufferSize(_context);
buf = new char[buffer_size];
{

0 comments on commit d5bec9c

Please sign in to comment.