Skip to content

Commit 0227e69

Browse files
Adding WordWrap code snippet.
0 parents  commit 0227e69

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

wordwrap/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle( "WordWrap" )]
9+
[assembly: AssemblyDescription( "" )]
10+
[assembly: AssemblyConfiguration( "" )]
11+
[assembly: AssemblyCompany( "Microsoft" )]
12+
[assembly: AssemblyProduct( "WordWrap" )]
13+
[assembly: AssemblyCopyright( "Copyright © Microsoft 2010" )]
14+
[assembly: AssemblyTrademark( "" )]
15+
[assembly: AssemblyCulture( "" )]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible( false )]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid( "b8031ab7-5919-424a-8e2c-d389c6c2444e" )]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion( "1.0.0.0" )]
36+
[assembly: AssemblyFileVersion( "1.0.0.0" )]

wordwrap/WordWrap.csproj

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>9.0.30729</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{EE18F070-27F8-465A-817C-0F08A5EC95C0}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>WordWrap</RootNamespace>
12+
<AssemblyName>WordWrap</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
35+
<Reference Include="System" />
36+
<Reference Include="System.Core">
37+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
38+
</Reference>
39+
<Reference Include="System.Xml.Linq">
40+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
41+
</Reference>
42+
<Reference Include="System.Data.DataSetExtensions">
43+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
44+
</Reference>
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<Compile Include="WordWrapTests.cs" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
</Project>

wordwrap/WordWrap.csproj.user

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<PropertyGroup>
3+
<ProjectView>ShowAllFiles</ProjectView>
4+
</PropertyGroup>
5+
</Project>

wordwrap/WordWrap.sln

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 10.00
3+
# Visual Studio 2008
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordWrap", "WordWrap.csproj", "{EE18F070-27F8-465A-817C-0F08A5EC95C0}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{EE18F070-27F8-465A-817C-0F08A5EC95C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{EE18F070-27F8-465A-817C-0F08A5EC95C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{EE18F070-27F8-465A-817C-0F08A5EC95C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{EE18F070-27F8-465A-817C-0F08A5EC95C0}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

wordwrap/WordWrap.suo

8.5 KB
Binary file not shown.

wordwrap/WordWrapTests.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace WordWrapSnippet
6+
{
7+
[TestClass]
8+
public class WordWrapTests
9+
{
10+
public static List<string> WordWrap( string text, int maxLineLength )
11+
{
12+
var list = new List<string>();
13+
14+
int currentIndex;
15+
var lastWrap = 0;
16+
var whitespace = new[] { ' ', '\r', '\n', '\t' };
17+
do
18+
{
19+
currentIndex = lastWrap + maxLineLength > text.Length ? text.Length : (text.LastIndexOfAny( new[] { ' ', ',', '.', '?', '!', ':', ';', '-', '\n', '\r', '\t' }, Math.Min( text.Length - 1, lastWrap + maxLineLength) ) + 1);
20+
if( currentIndex <= lastWrap )
21+
currentIndex = Math.Min( lastWrap + maxLineLength, text.Length );
22+
list.Add( text.Substring( lastWrap, currentIndex - lastWrap ).Trim( whitespace ) );
23+
lastWrap = currentIndex;
24+
} while( currentIndex < text.Length );
25+
26+
return list;
27+
}
28+
29+
[TestMethod]
30+
public void WordWrap_SplitsTextIntoAllowableLengthsAndAvoidsBreakingWords()
31+
{
32+
var rows = WordWrap( "12345 abcd 1234", 7 );
33+
Assert.AreEqual(3, rows.Count);
34+
Assert.AreEqual("12345", rows[0] );
35+
Assert.AreEqual("abcd", rows[1] );
36+
Assert.AreEqual("1234", rows[2] );
37+
}
38+
39+
[TestMethod]
40+
public void WordWrap_BreaksWordExceedingLineLimit()
41+
{
42+
var rows = WordWrap( "foobarhello", 7 );
43+
Assert.AreEqual(2, rows.Count );
44+
Assert.AreEqual("foobarh", rows[0] );
45+
Assert.AreEqual("ello", rows[1] );
46+
}
47+
48+
[TestMethod]
49+
public void WordWrap_BreaksWordExceedingLimitOfSeveralLines()
50+
{
51+
var rows = WordWrap( "foobar_line_foobar", 6 );
52+
Assert.AreEqual(3, rows.Count );
53+
Assert.AreEqual( "foobar", rows[0] );
54+
Assert.AreEqual( "_line_", rows[1] );
55+
Assert.AreEqual( "foobar", rows[2] );
56+
}
57+
58+
[TestMethod]
59+
public void WordWrap_BreaksWordsExceedingLineLimitAndBreaksBetweenWordThatFitsWithinLimit()
60+
{
61+
var rows = WordWrap( "lorem ipsum testing 12345 abc", 12 );
62+
Assert.AreEqual( 3, rows.Count );
63+
Assert.AreEqual( "lorem ipsum", rows[0] );
64+
Assert.AreEqual( "testing", rows[1]);
65+
Assert.AreEqual( "12345 abc", rows[2] );
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)